简体   繁体   中英

How to fix not show any items in RecyclerView on Android

In my application I should show some data into RecyclerView .
I want send item count to adapter and then show items.
I write below code, but not show me any item!!!

Activity Code:

call.enqueue(new Callback<CelebrityAwardResponse>() {
    @Override
    public void onResponse(Call<CelebrityAwardResponse> call, Response<CelebrityAwardResponse> response) {
        if (response.body().getData() != null) {
            awardModel.clear();
            List<Award> won = new ArrayList<>();
            List<Award> lost = new ArrayList<>();
            for (Award award : response.body().getData().get(1).getAwards()) {
                if (!award.getWon()) {
                    lost.add(award);
                } else {
                    won.add(award);
                }
            }
            if (won.size() > 0) {
                awardModel.addAll(won);
                Log.e("Won", "G won : " + won.size());
            } else {
                awardModel.addAll(lost);
                Log.e("Won", "G lost : " + lost.size());
            }

            awardAdapter = new AwardAdapter(context, awardModel, won.size(), lost.size());
            infoMovieFrag_AwardGoldenRecyclerView.setLayoutManager(new LinearLayoutManager
                    (context, LinearLayoutManager.HORIZONTAL, false));
            infoMovieFrag_AwardGoldenRecyclerView.setHasFixedSize(true);
            infoMovieFrag_AwardGoldenRecyclerView.setNestedScrollingEnabled(false);
            infoMovieFrag_AwardGoldenRecyclerView.addOnItemTouchListener(disableRecyclerViewScroll);
            infoMovieFrag_AwardGoldenRecyclerView.setAdapter(awardAdapter);
        }
    }

Adapter code:

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

    private Context context;
    private List<Award> model;
    private int modelImage;
    private int won, nominated, total;

    public AwardAdapter(Context context, List<Award> model, int won, int nominated) {
        this.context = context;
        this.model = model;
        this.won = won;
        this.nominated = nominated;
    }

    @Override
    public AwardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_award, parent, false);

        return new AwardAdapter.ViewHolder(view);
    }

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

        if (model.get(position).getWon()) {
            modelImage = R.drawable.golden_globe_gold;
        } else {
            modelImage = R.drawable.golden_globe_silver;
        }
        Glide.with(context)
                .load(modelImage)
                .placeholder(R.drawable.default_image)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(holder.row_awardImg);

        if (won > 0) {
            total = won;
        } else {
            total = nominated;
        }

        Log.e("AwardNumber", total + "");
    }

    @Override
    public int getItemCount() {
        return total;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView row_awardImg;

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

            row_awardImg = (ImageView) itemView.findViewById(R.id.row_awardImg);

        }
    }
}

Why not show data? Please help me

It seems like that the items count is zero since your getItemCount is returning total which does not depend on the size of the list. You should change this to:

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

EDIT:

Move this code to the constructor of your class:

public AwardAdapter(Context context, List<Award> model, int won, int nominated) {
    this.context = context;
    this.model = model;
    this.won = won;
    this.nominated = nominated;

    if (won > 0) {
        total = won;
    } else {
        total = nominated;
    }
}

Then you can use total with getItemCount .

Change your getItemCount() method as follows

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

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