Android : How to get a specific value from an ArrayList by position in listview?

on Tuesday, July 8, 2014


Title might be a bit confusing and if so, sorry about that (lack of sleep does that sometimes).


So, I have a defined ArrayList that is used in my adapter to populate my listview, as seen below:


ArrayList:



oslist = new ArrayList<HashMap<String, String>>();
oslist = Product.getArrayList();
if (intent.getStringExtra("PROD_NAME")!= null) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(PROD_NAME, name);
map.put(PROD_PRICE, price);
oslist.add(map);


Adapter:



adapter = new SimpleAdapter(shopping_cart.this, oslist,
R.layout.shoppingcart,
new String[] {PROD_NAME, PROD_PRICE}, new int[] {R.id.name, R.id.Price});
setListAdapter(adapter);
registerForContextMenu(lv);


I also created a context menu that has one purpose only - to delete the selected item from the list, as such:



@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.remove_item:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
position = (int) info.id;

total = " Total : "+price_total+" RON ";
total_tv.setText(total);

oslist.remove(position);
((BaseAdapter) this.adapter).notifyDataSetChanged();
return true;
}
return super.onContextItemSelected(item);
}


My problem: How exactly can I get the product price from the arraylist by its position before removing the item? I need to get the price so I can subtract it from the total.


Thanks in advance and let me know if you need additional information and I'll edit my post ASAP.


0 comments:

Post a Comment