I am trying to develop a simple order app.
Here I want to add/cancel the product
ListView/GridView reuses views. What this means in your case is that when e.g. you have 1 Pizza, you scroll down so the Pizza disappears, the listview reuses that view for the next view that appears, so you'll automatically have a Items: 1 that was left over from the Pizza's view.
What you need to to is store the count for every item and set tvCounter accordingly in getView.
So, Finally I found the solution for scroll issue..
vi.setOnClickListener(new OnItemClickListener(position));
final int pos = position;
holder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View parentView = (View) v.getParent();
int totAmount = Integer.parseInt(((TextView) parentView
.findViewById(R.id.text1)).getText()
.toString());
count = Integer.parseInt(((TextView) parentView
.findViewById(R.id.counter)).getText().toString());
count++;
tot_amt = totAmount * count;
((TextView) parentView.findViewById(R.id.counter))
.setText(String.valueOf(count));
((TextView) parentView.findViewById(R.id.totalPrice))
.setText(String.valueOf(tot_amt));
//Here I am saving the total amount and quantity
tempValues = null;
tempValues = (ListModel) data.get(pos);
tempValues.setProductName(tempValues.getProductName());
tempValues.setPrice(tempValues.getPrice());
tempValues.setImage(tempValues.getImage());
tempValues.setQuantity(String.valueOf(count));
tempValues.setTotalAmount(String.valueOf(tot_amt));
data.set(pos, tempValues);
ListViewExample sct = (ListViewExample) activity;
sct.getAllValues();
}
});
holder.decreesButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View parentView = (View) v.getParent();
int totAmount = Integer.parseInt(((TextView) parentView
.findViewById(R.id.text1)).getText()
.toString());
count = Integer.parseInt(((TextView) parentView
.findViewById(R.id.counter)).getText().toString());
if (count > 0) {
count--;
tot_amt = totAmount * count;
((TextView) parentView.findViewById(R.id.totalPrice))
.setText(String.valueOf(tot_amt));
((TextView) parentView.findViewById(R.id.counter))
.setText(String.valueOf(count));
//Here I am saving the total amount and quantity
tempValues = null;
tempValues = (ListModel) data.get(pos);
tempValues.setProductName(tempValues
.getProductName());
tempValues.setPrice(tempValues.getPrice());
tempValues.setImage(tempValues.getImage());
tempValues.setQuantity(String.valueOf(count));
tempValues.setTotalAmount(String.valueOf(tot_amt));
data.set(pos, tempValues);
ListViewExample sct = (ListViewExample) activity;
sct.getAllValues();
}
}
});
Thanks Longi..!!