Reputation: 6888
I have three Items in a ListView, and on each and every list item i have three TextView(s), one contains item price, second to show Quantity and third to show Total of that item.
Also two more TextView(s), one to increase the quantity and second to decrease the quantity.
Now the issue is whenever i make changes in quantity of first or second list item, it effects on quantity and total amount of third list item (i.e - Last item in a List)
public class CartAdapter extends BaseAdapter {
ViewHolder holder;
......
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = layoutInflater.inflate(Resource, null);
holder.textViewPrice = (TextView) v.findViewById(R.id.textPrice);
holder.editTextQuantity = (EditText) v.findViewById(R.id.editQuantity);
holder.textViewTotal = (TextView) v.findViewById(R.id.textTotal);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.textViewPrice.setText("$ " +cartArrayList.get(Integer.parseInt(holder.textViewPrice.getTag())).getPrice());
cart = cartArrayList.get(Integer.parseInt(v.getTag()));
holder.editTextQuantity.setText(String.valueOf(cart.getQuantity()));
df = new DecimalFormat("0.00##");
totalPrice = cart.getQuantity() * cart.getPrice();
cart.setTotal(totalPrice);
holder.textViewTotal.setText("$ " + df.format(cart.getTotal()));
return v;
}
Upvotes: 0
Views: 513
Reputation: 1917
Initialize your ViewHolder
object at start of getView
method.
Upvotes: 1
Reputation: 5661
There is only one holder variable in your base adapter. It's getting updated with each call to getView(). THe problem is that your click listeners are using that holder variable to update the views. SInce the holder is now pointing to the last view item that was created, that's the one that's getting updated in the onClick.
A simple way to solve the problem is to create a final ViewHolder variable and use it in the onClicks. You can use the same variable for both OnClickListeners.
like so..
final ViewHolder perItemHolder = holder;
holder.textQtyIncrease.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(p == 9) {
Toast.makeText(context, "You have reached to maximum order quantity", Toast.LENGTH_LONG).show();
return ;
}
else {
p = p+1;
perItemHolder.textViewQuantity.setText(""+p);
totalPrice = p * cart.getPrice();
cart.setTotal(totalPrice);
perItemHolder.textViewTotal.setText("$ " + df.format(cart.getTotal()));
}
}
});
Upvotes: 0
Reputation: 1373
try this way
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = layoutInflater.inflate(Resource, null);
holder.textViewPrice = (TextView) v.findViewById(R.id.textPrice);
holder.editTextQuantity = (EditText) v.findViewById(R.id.editQuantity);
holder.textViewTotal = (TextView) v.findViewById(R.id.textTotal);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
v.setTag("ID",position);
holder.textViewPrice.setTag(position);
holder.editTextQuantity.setTag(position);
holder.textViewTotal.setTag(position);
holder.textViewPrice.setText("$ " +cartArrayList.get(Integer.parseInt(holder.textViewPrice.getTag())).getPrice());
cart = cartArrayList.get(Integer.parseInt(v.getTag()));
holder.editTextQuantity.setText(String.valueOf(cart.getQuantity()));
df = new DecimalFormat("0.00##");
totalPrice = cart.getQuantity() * cart.getPrice();
cart.setTotal(totalPrice);
holder.textViewTotal.setText("$ " + df.format(cart.getTotal()));
return v;
}
Upvotes: 0