gpt4 book ai didi

java - RecyclerView 正在复制列表中的前三个项目

转载 作者:太空宇宙 更新时间:2023-11-04 09:45:35 24 4
gpt4 key购买 nike

我对 android 相当陌生,我正在尝试使用 RecyclerView 来显示 firebase 上托管的内容,但是当它出现时,前三个项目是重复的。

我尝试了一些解决方案,但似乎都不起作用,任何帮助都会很棒!

DiscountRecyclerAdapter.java

public class DiscountRecyclerAdapter extends RecyclerView.Adapter<com.cianod.comharapp.DiscountRecyclerAdapter.ViewHolder> {

public List<Discounts> discountsList;
public Context context;
private ImageView discountImageView;

public DiscountRecyclerAdapter(List<Discounts> discountsList){

this.discountsList = discountsList;

}

@Override
public com.cianod.comharapp.DiscountRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.discount_list_item, parent, false);
context = parent.getContext();
return new com.cianod.comharapp.DiscountRecyclerAdapter.ViewHolder(view);
}

@Override
public void onBindViewHolder(final com.cianod.comharapp.DiscountRecyclerAdapter.ViewHolder holder, int position) {

holder.setIsRecyclable(false);

String discountName = discountsList.get(position).getDiscount_name();
holder.setDiscount_Name(discountName);

String discountDescription = discountsList.get(position).getDiscount_description();
holder.setDiscount_Description(discountDescription);

String discountValue = discountsList.get(position).getDiscount_value();
holder.setDiscount_Value(discountValue);


String image_url = discountsList.get(position).getDiscount_image();
String thumbUri = discountsList.get(position).getDiscount_image();
holder.setDiscount_Image(image_url, thumbUri);

}


@Override
public int getItemCount() {

if(discountsList != null) {

return discountsList.size();

} else {

return 0;

}

}

public class ViewHolder extends RecyclerView.ViewHolder {

private View mView;

private TextView discount_name;

public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
}

public void setDiscount_Name(String message){

discount_name = mView.findViewById(R.id.discount_name);
discount_name.setText(message);

}

public void setDiscount_Description(String message){

discount_name = mView.findViewById(R.id.discount_description);
discount_name.setText(message);

}

public void setDiscount_Value(String message){

discount_name = mView.findViewById(R.id.discount_value);
discount_name.setText(message);

}

public void setDiscount_Image(String downloadUri, String thumbUri){

discountImageView = mView.findViewById(R.id.discount_image);

RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.image_placeholder);

Glide.with(context).applyDefaultRequestOptions(requestOptions).load(downloadUri).thumbnail(
Glide.with(context).load(thumbUri)
).into(discountImageView);

}

}

}

我相信问题出在折扣适配器上,但我不能肯定。 RecyclerView 显示在 fragment 上(如果这可能会影响它),如果需要,我可以附加其他 fragment 。

最佳答案

您的代码没有任何问题。所以也许您应该尝试覆盖适配器类中的这两个方法

@Override
public long getItemId(int position) {
return position;
}

@Override
public int getItemViewType(int position) {
return position;
}

关于java - RecyclerView 正在复制列表中的前三个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55464910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com