繁体   English   中英

从服务器加载数据时,如何设置空适配器并将适配器重新绑定到RecyclerView

[英]How to set empty adapter and re-bind adapter to RecyclerView when data load from server

如何在访问fragment时注册一个empty adapters ,并在服务器接收到数据后如何将其重新绑定到RecyclerView

我仍然对将适配器设置为空感到困惑,然后当我连接到数据库时,当我使用RecyclerView时结果将可见

也许有人可以教我如何实现这一目标

我的适配器

package adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.transvision.bertho.transvisiondashboardapp.R;

import java.util.List;

import model.Channel;


public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.ChannelViewHolder> {

    private List<Channel> channels;
    private int rowLayout;
    private Context context;

    public static class ChannelViewHolder extends RecyclerView.ViewHolder {

        LinearLayout moviesLayout;
        TextView movieTitle;
        TextView data;
        TextView movieDescription;
        TextView rating;

        TextView name;
        TextView code;
        TextView description;
        TextView number;
        TextView definition;
        TextView paket;
        ImageView logo;

        public ChannelViewHolder(View v) {
            super(v);
            moviesLayout = (LinearLayout) v.findViewById(R.id.movies_layout);
            name = (TextView) v.findViewById(R.id.title);
            definition = (TextView) v.findViewById(R.id.subtitle);
            description = (TextView) v.findViewById(R.id.description);
        }
    }

    public ChannelAdapter(List<Channel> channels, int rowLayout, Context context) {
        this.channels = channels;
        this.rowLayout = rowLayout;
        this.context = context;
    }

    @Override
    public ChannelAdapter.ChannelViewHolder onCreateViewHolder(ViewGroup parent,
                                                            int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
        return new ChannelViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ChannelViewHolder holder, final int position) {
        holder.name.setText(channels.get(position).getName());
        holder.definition.setText(channels.get(position).getDefinition());
        holder.description.setText(channels.get(position).getDescription());
    }

    @Override
    public int getItemCount() {
        return channels.size();
    }

}

我的片段

package page;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.transvision.bertho.transvisiondashboardapp.R;

import java.util.ArrayList;
import java.util.List;

import adapter.ChannelAdapter;
import model.Channel;
import model.ChannelResponse;
import rest.ApiClient;
import rest.ApiInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class HomeFragment extends Fragment {

    private RecyclerView recyclerView;
    private static final String TAG = HomeFragment.class.getSimpleName();

    private ChannelAdapter adapter;
    List<Channel> listChannel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);


        adapter = new ChannelAdapter (listChannel, R.layout.list_channel, getActivity());

        recyclerView = (RecyclerView) rootView.findViewById(R.id.movies_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


        getChannelData();

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public void getChannelData() {
        ApiInterface apiService = ApiClient.getChannel().create(ApiInterface.class);
        Call<ChannelResponse> call = apiService.getItems();

        call.enqueue(new Callback<ChannelResponse>() {
            @Override
            public void onResponse(Call<ChannelResponse> call, Response<ChannelResponse> response) {
                int statusCode = response.code();
                List<Channel> channel = response.body().getItems();
                recyclerView.setAdapter(new ChannelAdapter(channel, R.layout.list_channel, getActivity()));
                adapter.notifyDataSetChanged();
                showToast("CONNECTION SUCCESS");
            }

            @Override
            public void onFailure(Call<ChannelResponse> call, Throwable t) {
                // Log error here since request failed
                Log.e(TAG, t.toString());
                showToast("CONNECTION ERROR");
            }
        });
    }

    public void showToast(String output){
        Toast.makeText(getActivity(), output, Toast.LENGTH_SHORT).show();
    }
}

通过使用notifyDataSetChanged()向适配器通知数据已更改的正确方法又如何呢?

请帮助我了解过程

谢谢

使用空的频道列表初始化ChannelAdapter并将其设置为recyclerview。 然后,当您从api接收频道列表时,请更新适配器中的列表,然后调用notifyDataSetChanged()。 这将更新recyclerview。 您不必创建新的适配器并重新设置。

希望您能得到想要的东西:在适配器中,

public void addItems(List<SomeTeam> list){
    this.list.addAll(list);
    notifyDataSetChanged();
}

public void clearItems(){
    teams.clear();
}

您可以在将适配器中的列表设置为时在活动中调用此方法:

public void setSomeTeamList(List<SomeTeam>teamList){
    teamAdapter.clearItems();
    teamAdapter.addItems(teamList);
}

希望您能从上面的示例代码中得到启发。

首先初始化您的列表

List<Channel> listChannel = new ArrayList<Channel>();

现在在您的getChannelData()您正在创建新适配器,同时将其设置为recyclerView ,尽管您已经在onCreateView()创建了它

recyclerView.setAdapter(adapter); //pass your adapter object instead of creating new object.
adapter.notifyDataSetChanged();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM