向下滚动时 ListView 项目中的重复值
Duplicated value in item of ListView when scrolldown
我在列表视图 OnClickListener() 中有一个增加的按钮,这个按钮在项目增加的数量中显示在测试视图中,问题是当我向下滚动列表视图中的另一个项目更改为增加的相同数量。
这是我的 Adpater
public class ProductsAdapter extends BaseAdapter {
List<ProductSubModel> myList;
LayoutInflater inflater;
Context context;
int i;
public ProductsAdapter(Context context, List<ProductSubModel> myList) {
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return myList.size();
}
@Override
public Object getItem(int position) {
return myList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
final MyViewHolder mViewHolder;
i = 0;
if (convertView == null) {
convertView = inflater.inflate(R.layout.adapter_product, viewGroup, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
mViewHolder.productImageView.setScaleType(ImageView.ScaleType.FIT_XY);
Glide.with(context)
.load(myList.get(position).getImage())
.placeholder(R.drawable.nadarah_icon)
.override(250, 250)
.into(mViewHolder.productImageView);
mViewHolder.productNameTextView.setText(myList.get(position).getProductName());
mViewHolder.productDescribtionTextView.setText("DESC : " + myList.get(position).getShortDescription());
mViewHolder.productPriceTextView.setText("PRISE : " + myList.get(position).getPrice());
mViewHolder.discountPriceTextView.setText("DISCOUNT : " + myList.get(position).getMinimalPrice());
mViewHolder.qtyFromApiTextView.setText("QTY : " + 20);
mViewHolder.minusImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (i > 0) {
i--;
mViewHolder.qtyUserBuyTextView.setText("" + i);
} else {
Utils.msg(context , "");
}
}
});
mViewHolder.plusImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (i < 20) {
i++;
mViewHolder.qtyUserBuyTextView.setText("" + i);
} else {
Utils.msg(context , "");
}
}
});
mViewHolder.addtoCartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
return convertView;
}
private class MyViewHolder {
ImageView productImageView;
TextView productNameTextView;
TextView productDescribtionTextView;
TextView productPriceTextView;
TextView discountPriceTextView;
TextView qtyFromApiTextView;
ImageView minusImageView;
ImageView plusImageView;
TextView qtyUserBuyTextView;
TextView addtoCartButton;
TextView productIdTextView;
public MyViewHolder(View item) {
productImageView = (ImageView) item.findViewById(R.id.productImageView);
productNameTextView = (TextView) item.findViewById(R.id.productNameTextView);
productDescribtionTextView = (TextView) item.findViewById(R.id.productDescribtionTextView);
productPriceTextView = (TextView) item.findViewById(R.id.productPriceTextView);
discountPriceTextView = (TextView) item.findViewById(R.id.discountPriceTextView);
qtyFromApiTextView = (TextView) item.findViewById(R.id.qtyFromApiTextView);
minusImageView = (ImageView) item.findViewById(R.id.minusImageView);
qtyUserBuyTextView = (TextView) item.findViewById(R.id.qtyUserBuyTextView);
plusImageView = (ImageView) item.findViewById(R.id.plusImageView);
addtoCartButton = (TextView) item.findViewById(R.id.addtoCartButton);
productIdTextView = (TextView) item.findViewById(R.id.productIdTextView);
}
}
}
这是适配器实现
private void implementAdpater(List<ProductSubModel> productSubModels) {
this.productSubModels = productSubModels;
productsAdapter = new ProductsAdapter(getActivity(), productSubModels);
productsInCategoryList.setAdapter(productsAdapter);
productsAdapter.notifyDataSetChanged();
}
删除 productsAdapter.notifyDataSetChanged();在 setadapter() 之后检查一次。
检查此以供参考:
http://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial
ProductSubModel 中应该有一个 Quantity 字段(或任何增加或减少的字段),每次增加时模型也会增加。
i++;
mViewHolder.qtyUserBuyTextView.setText("" + i);
myList.get(position).setQunatity(myList.get(position).getQunatity()+1);
听众之外
mViewHolder.qtyUserBuyTextView.setText("" + myList.get(position).getQunatity());
同减
我在列表视图 OnClickListener() 中有一个增加的按钮,这个按钮在项目增加的数量中显示在测试视图中,问题是当我向下滚动列表视图中的另一个项目更改为增加的相同数量。
这是我的 Adpater
public class ProductsAdapter extends BaseAdapter {
List<ProductSubModel> myList;
LayoutInflater inflater;
Context context;
int i;
public ProductsAdapter(Context context, List<ProductSubModel> myList) {
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return myList.size();
}
@Override
public Object getItem(int position) {
return myList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
final MyViewHolder mViewHolder;
i = 0;
if (convertView == null) {
convertView = inflater.inflate(R.layout.adapter_product, viewGroup, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
mViewHolder.productImageView.setScaleType(ImageView.ScaleType.FIT_XY);
Glide.with(context)
.load(myList.get(position).getImage())
.placeholder(R.drawable.nadarah_icon)
.override(250, 250)
.into(mViewHolder.productImageView);
mViewHolder.productNameTextView.setText(myList.get(position).getProductName());
mViewHolder.productDescribtionTextView.setText("DESC : " + myList.get(position).getShortDescription());
mViewHolder.productPriceTextView.setText("PRISE : " + myList.get(position).getPrice());
mViewHolder.discountPriceTextView.setText("DISCOUNT : " + myList.get(position).getMinimalPrice());
mViewHolder.qtyFromApiTextView.setText("QTY : " + 20);
mViewHolder.minusImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (i > 0) {
i--;
mViewHolder.qtyUserBuyTextView.setText("" + i);
} else {
Utils.msg(context , "");
}
}
});
mViewHolder.plusImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (i < 20) {
i++;
mViewHolder.qtyUserBuyTextView.setText("" + i);
} else {
Utils.msg(context , "");
}
}
});
mViewHolder.addtoCartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
return convertView;
}
private class MyViewHolder {
ImageView productImageView;
TextView productNameTextView;
TextView productDescribtionTextView;
TextView productPriceTextView;
TextView discountPriceTextView;
TextView qtyFromApiTextView;
ImageView minusImageView;
ImageView plusImageView;
TextView qtyUserBuyTextView;
TextView addtoCartButton;
TextView productIdTextView;
public MyViewHolder(View item) {
productImageView = (ImageView) item.findViewById(R.id.productImageView);
productNameTextView = (TextView) item.findViewById(R.id.productNameTextView);
productDescribtionTextView = (TextView) item.findViewById(R.id.productDescribtionTextView);
productPriceTextView = (TextView) item.findViewById(R.id.productPriceTextView);
discountPriceTextView = (TextView) item.findViewById(R.id.discountPriceTextView);
qtyFromApiTextView = (TextView) item.findViewById(R.id.qtyFromApiTextView);
minusImageView = (ImageView) item.findViewById(R.id.minusImageView);
qtyUserBuyTextView = (TextView) item.findViewById(R.id.qtyUserBuyTextView);
plusImageView = (ImageView) item.findViewById(R.id.plusImageView);
addtoCartButton = (TextView) item.findViewById(R.id.addtoCartButton);
productIdTextView = (TextView) item.findViewById(R.id.productIdTextView);
}
}
}
这是适配器实现
private void implementAdpater(List<ProductSubModel> productSubModels) {
this.productSubModels = productSubModels;
productsAdapter = new ProductsAdapter(getActivity(), productSubModels);
productsInCategoryList.setAdapter(productsAdapter);
productsAdapter.notifyDataSetChanged();
}
删除 productsAdapter.notifyDataSetChanged();在 setadapter() 之后检查一次。
检查此以供参考: http://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial
ProductSubModel 中应该有一个 Quantity 字段(或任何增加或减少的字段),每次增加时模型也会增加。
i++;
mViewHolder.qtyUserBuyTextView.setText("" + i);
myList.get(position).setQunatity(myList.get(position).getQunatity()+1);
听众之外
mViewHolder.qtyUserBuyTextView.setText("" + myList.get(position).getQunatity());
同减