简体   繁体   中英

How to set data in RecyclerView on Android

I want get some data from server and show it into Recyclerview . I can show this data, but some time not show this data and just show progressBar.
I show this data in Log.e and show fastly , but not show this data in RecyclerView and just show Progress.
Please see below picture to know my mean :
Click too see

My Response codes:

InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<CountryResponse> call = api.getCountryList();

call.enqueue(new Callback<CountryResponse>() {
    @Override
    public void onResponse(Call<CountryResponse> call, Response<CountryResponse> response) {
        try {
            if (response.body() != null) {
                models.clear();
                models.addAll(response.body().getData());
                for (int i = 0; i <= models.size(); i++) {
                    Log.e("CountryInfoTAG", response.body().getData().get(i).getId() + " : " +
                            response.body().getData().get(i).getName());
                }

                countryProgress.setVisibility(View.GONE);
                countryRecycler.setAdapter(mAdapter);
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void onFailure(Call<CountryResponse> call, Throwable t) {
        Toasty.error(context, context.getResources().getString(R.string.failRequest),
                Toast.LENGTH_LONG, true).show();
    }
});

Adapter codes:

public class CountryAdapter extends RecyclerView.Adapter {

    private List<CountryDatum> mData;
    private Context context;
    private sendDataListener listner;

    public CountryAdapter(List<CountryDatum> mData, Context context, sendDataListener listner) {
        this.mData = mData;
        this.context = context;
        this.listner = listner;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder vh;
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_country, parent, false);
        vh = new DataViewHolder(v);

        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof DataViewHolder) {
            ((DataViewHolder) holder).countryListTxt.setText(mData.get(position).getName() + "");
            ((DataViewHolder) holder).countryListTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listner.onSendIdName(mData.get(position).getId(), mData.get(position).getName());

                    if (context instanceof RegisterActivity) {
                        ((RegisterActivity) context).dismissCountryDialog();
                    }
                }
            });
        }
    }

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

    public void add(List<CountryDatum> models) {
        mData.addAll(models);
        notifyDataSetChanged();
    }

    public void clear() {
        mData.clear();
        notifyDataSetChanged();
    }

    public class DataViewHolder extends RecyclerView.ViewHolder {
        private TextView countryListTxt;

        public DataViewHolder(View itemView) {
            super(itemView);

            countryListTxt = (TextView) itemView.findViewById(R.id.countryNameTxt);
        }
    }
}

How can I fix? please help me. How can I fix? please help me

Well Try this, you are not initializing the adapter object in success response. Add this mAdapter = new CountryAdapter(models,getActivity(),listener);

@Override
    public void onResponse(Call<CountryResponse> call, Response<CountryResponse> response) {
        try {
            if (response.body() != null) {
                models.clear();
                models.addAll(response.body().getData());
                for (int i = 0; i <= models.size(); i++) {
                    Log.e("CountryInfoTAG", response.body().getData().get(i).getId() + " : " +
                            response.body().getData().get(i).getName());
                }

                countryProgress.setVisibility(View.GONE);
                mAdapter = new CountryAdapter(models,getActivity(),listener);
                countryRecycler.setAdapter(mAdapter);
            }
        } catch (Exception e) {
        }
    }

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