在我的应用程序中,我应该向RecyclerView
显示一些数据
我想将项目计数发送到adapter
,然后显示项目
我写下面的代码,但不显示我任何项目!!!
活动代码:
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);
}
}
适配器代码:
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);
}
}
}
为什么不显示数据?请帮帮我
答案 0 :(得分:0)
似乎项目计数为零,因为您的getItemCount
正在返回total
,这不依赖于列表的大小。您应该将其更改为:
@Override
public int getItemCount() {
return model.size();
}
修改强>
将此代码移动到您的类的构造函数:
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;
}
}
然后,您可以将total
与getItemCount
一起使用。
答案 1 :(得分:0)
按如下方式更改getItemCount()
方法
@Override
public int getItemCount() {
return model.size();
}