简体   繁体   English

如何修复在Android上的RecyclerView中不显示任何项目

[英]How to fix not show any items in RecyclerView on Android

In my application I should show some data into RecyclerView . 在我的应用程序中,我应该向RecyclerView显示一些数据。
I want send item count to adapter and then show items. 我想将项目计数发送到adapter ,然后显示项目。
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. 由于您的getItemCount返回的total不依赖于列表的大小,因此项目计数似乎为零。 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 . 然后,可以将totalgetItemCount一起使用。

Change your getItemCount() method as follows 如下更改您的getItemCount()方法

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

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

相关问题 如何修复 Recyclerview 返回相同的项目 android - How to fix Recyclerview returns same items android Android-如何更改RecyclerView的小部件的可见性并将其显示在recyclerView的所有项目中? - Android - How to change the visibility of a widget of and show it in all items in recyclerView? 如何在Android RecyclerView中显示一组项目的日期/日期 - How to show day/date above of a group of items in Android RecyclerView RecyclerView没有显示任何项目 - RecyclerView doesn't show any items 如何在Android上先显示Horizo​​ntal RecyclerView的前10个项目,然后滑动以显示另外10个项目? - How to show Horizontal RecyclerView first 10 items and then swipe to show another 10 items in android? Android Studio RecyclerView 不显示项目 - Android Studio RecyclerView does not show items Android RecyclerView 不显示项目 - Android RecyclerView doesn't show items 当我在 recyclerview 中删除一个项目然后添加一个新项目时,我删除的项目再次出现在我的 Android 应用程序中。我该如何修复它? 有什么解决办法吗? - When I delete an item in recyclerview and then add a new item, the items I deleted appear again in my Android App.How can I fix it? Any solution? 如何在Android中禁用RecyclerView项目? - How to disable RecyclerView items in Android? 如何在Android中向recyclerView添加项目 - How to add items to recyclerView in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM