Malik Abu Qaoud Malik Abu Qaoud - 15 days ago 5
Android Question

Duplicated value in item of ListView when scrolldown

I have an increasing button in listview OnClickListener() of this button in item increase number in show it in testview the problem is when i scrolldown anther item in the listview changes to the same number that increased.

This is My 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 , "btt.habal y3ne -___-");
}
}
});
mViewHolder.plusImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (i < 20) {
i++;
mViewHolder.qtyUserBuyTextView.setText("" + i);
} else {
Utils.msg(context , "fsh 3na akth.ar mn heek");
}
}
});
mViewHolder.addtoCartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
apiCall(position);
}
});
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);
}

}
}


This is adapter implementation

private void implementAdpater(List<ProductSubModel> productSubModels) {
this.productSubModels = productSubModels;
productsAdapter = new ProductsAdapter(getActivity(), productSubModels);
productsInCategoryList.setAdapter(productsAdapter);
productsAdapter.notifyDataSetChanged();
}

Answer

There should be a field in ProductSubModel of Quantity (or whatever is increasing or decreasing), and each time you increment also increment in model.

i++;
mViewHolder.qtyUserBuyTextView.setText("" + i);
myList.get(position).setQunatity(myList.get(position).getQunatity()+1);

Outside from the listener

mViewHolder.qtyUserBuyTextView.setText("" + myList.get(position).getQunatity());

same for decrement