2

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);
                mAdapter = new CountryAdapter(models, context, listener);
                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

5
  • How does this compile without casting response.body to an object? response.body().getData().get(i).getName()
    – Charlie
    May 23, 2017 at 17:29
  • Are you using Retrofit 2?
    – Charlie
    May 23, 2017 at 17:29
  • @Charlie, yes my friend, can you help me? I need this :(
    – user8022871
    May 23, 2017 at 17:30
  • So you have data & an adapter, do you have a recycler view declared in a layout of a fragment / activity already too?
    – Charlie
    May 23, 2017 at 17:34
  • @Charlie, yes my friend. I sure initialize recyclerview in activity. but not show data into recyclerview
    – user8022871
    May 23, 2017 at 17:36

3 Answers 3

1

Try to replace your adapter with this:

public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.ViewHolder> {

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 ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_country, parent, false);

    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        holder.countryListTxt.setText(mData.get(position).getName() + "");
        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 ViewHolder extends RecyclerView.ViewHolder {
    public TextView countryListTxt;

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

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

}

1
  • What was the change to fix the adapter?
    – Jorgesys
    Apr 16, 2021 at 18:56
0

Please update your onBindView method in adapter class, do not cast like this

((DataViewHolder) holder)

Try this

 @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        holder.countryListTxt.setText(mData.get(position).getName() + "");
            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();
                    }
                }
            });

    }

and also print the modal after setting adapter in your success method of your Wep API callback in activity class

0

The object vh that you return from onCreateViewHolder is defined as a RecyclerView.ViewHolder.

RecyclerView.ViewHolder vh;
View v = // ...
vh = new DataViewHolder(v);
return vh;

Therefore the following statement in onBindViewHolder could be returning false (it would be worth checking with the debugger).

if (holder instanceof DataViewHolder)

In order to fix it, try defining your class like this:

public class CountryAdapter extends RecyclerView.Adapter<DataViewHolder> {
    // ...
}

From here you will also need to change a couple of methods:

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

@Override
public void onBindViewHolder(DataViewHolder holder, final int position) {
    holder.countryListTxt.setText(mData.get(position).getName() + "");
    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();
                }
            }
        });
    }
}
3
  • can you send me full code of adapter with your codes? please my bro . I need this :(
    – user8022871
    May 23, 2017 at 18:02
  • are you here? can you help me? please my friend
    – user8022871
    May 23, 2017 at 18:08
  • Sorry, didn't see this until now. You fixed it?
    – Charlie
    May 24, 2017 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.