简体   繁体   中英

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

How to register an empty adapters when accessed fragment , and perform re-bind into RecyclerView when data has been received by the server?

I'm still confused to set the adapter that it is empty, then when I connect to the database, the result will be visible when I use RecyclerView

Maybe someone can teach me how to make it happen

My adapter

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();
    }

}

My Fragment

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();
    }
}

And what about the correct way to give notify to the adapter that the data has been changed by using notifyDataSetChanged()

Please help me to understand the process

thank you

Initialize ChannelAdapter with an empty list of channels and set it to recyclerview. Then when you receive channels list from api update the list in adapter and call notifyDataSetChanged(). This will update the recyclerview. You don't have to create a new adapter and set it again.

Hope you can get what you want like this: In adapter,

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

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

And you can call this in your activity while setting list in adapter as :

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

Hope you can get idea from above sample code.

First of all initialize your list

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

now in your getChannelData() you are creating new adapter while setting it to recyclerView , though you have already created it in onCreateView()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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