Reputation: 1059
Hello I hope you are able to understand me, my English is not good, sorry
, I have an activity
that contains a listview
of products,if you click is added to another activity shopping cart. So far so good. I have gotten that if you click 2 times an article, 2 same items will not be added in the shopping cart, but now I want to establish a method to show me the same item if I pressed 2 times, exit 2 in the quantity field , and the price multiply by two, logically.
So insert the clicked item in the cart activity:
I created a variable that counts how many times an item was clicked,(contador)
case 0:
if (page1 == null)
{
page1 = (ListView) LayoutInflater.from(Local_mostrar_bebidaActivity.this).inflate(R.layout.page_one_viewpager_listview, null);
page1.setAdapter(adapterlistvMenuBEBIDAS);
page1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ItemListView item = itemsListVMenuBEBIDAS.get(position);
if (MiPedidoActivity.itemsListVMiPedido.contains(item)) { // <- look for item!
// ... item already in list
Log.i(LOGTAG, "Añadido item "+ contador+" "+ numUds);
++contador;
numUds=contador;
}
else {
MiPedidoActivity.itemsListVMiPedido.add(item);
contador= 1;
numUds=contador;
Log.i(LOGTAG, "Añadido item "+ contador+" "+ numUds);
}});
}
page = page1;
break;
I so add the products in the arraylist
private static ArrayList<ItemListView> obtenerItemsMenuBEBIDAS() {
ArrayList<ItemListView> itemsMenuBEBIDAS = new ArrayList<ItemListView>();
itemsMenuBEBIDAS.add(new ItemListView(1, "Product1", "malta y z.", "drawable/a1","3.10",""+numUds));
itemsMenuBEBIDAS.add(new ItemListView(3, "Cocacola", "Botellin de 22ml","drawable/cocacola_logo","1.75",""+ numUds));
itemsMenuBEBIDAS.add(new ItemListView(4, "Cocacola Zero", "Botellin de 22ml","drawable/cocacola_zero","1.75",""+ numUds ));
return itemsMenuBEBIDAS;
}
(numUds) is a variable that I have created to know how many units have been clicked and then display the corresponding textview
Any idea?
Upvotes: 0
Views: 4241
Reputation: 8180
I'm not sure where you're declaring both 'numUds' and 'contador' variables, but they seem to be variables for your Activity class, not for each item in the list.
You seem to have a class called ItemListView
that represent each object in the list. In that class you must have a property that keeps track of the number of units ('numUds' maybe) for each object. This way you can increment the count variable like this:
ItemListView item = itemsListVMenuBEBIDAS.get(position);
item.numUds ++;
Upvotes: 1