I just want to know how to, if a Hashmap is already on the list add 1 to quantity, if it is not then add it to list. This is what I've done that just add eventhough the item is already on list.
list = new ArrayList<HashMap<String, String>>();
Cursor c = db.rawQuery("SELECT code,desc,price FROM TbLPrice WHERE code =" + txtCode.getText().toString(), null); //search database
if (c.moveToFirst()){ //if successful
txtDesc.setText(c.getString(1)); //get desc
txtPrice.setText(c.getString(2)); // get price @ database
HashMap<String, String> map = new HashMap<String, String>();
map.put("desc", c.getString(1));
map.put("price", c.getString(2));
map.put("quantity","1");
list.add(map);
adapter.notifyDataSetChanged();
From your last comment, I think I see what you're trying to do. Firstly, I would create a small class holding the information about your items, to make it a bit easier to work with (I've only implemented the necessary setters, you'll probably want some getters (and other functions) as well):
public class MyItem
{
String description;
float price;
int quantity;
public void setDescription(String description)
{
this.description = description;
}
public void setPrice(float price)
{
this.price = price;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
public void increaseQuantity()
{
this.quantity++;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((description == null) ? 0 : description.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyItem other = (MyItem) obj;
if (description == null)
{
if (other.description != null)
return false;
}
else if (!description.equals(other.description))
return false;
return true;
}
}
I have implement the equals
method (and it is common to then also implement the hash
method) to easily be able to check if an item exists in the list (for simplicity, I assume that the description
uniquely identifies an item, you should change this as needed). You can then continue with your processing like this:
public void queryForItem(String itemCode)
{
Cursor cursor = db.rawQuery("SELECT code,desc,price FROM TbLPrice WHERE code =" + itemCode, null);
if (cursor.moveToFirst())
{
processCursor(cursor);
}
cursor.close();
}
private void processCursor(Cursor c)
{
MyItem newItem = new MyItem();
newItem.setDescription(c.getString(1));
newItem.setPrice(c.getFloat(2));
newItem.setQuantity(1);
// assuming that items (ArrayList<MyItem>) is defined and initialized earlier in the code
int existingItemIndex = items.indexOf(newItem);
if (existingItemIndex >= 0)
{
items.get(existingItemIndex).increaseQuantity();
}
else
{
items.add(newItem);
}
adapter.notifyDataSetChanged();
}
I haven't tested this in any way, but I thing it should do what you want. Hope you're able to see the logic in it :)
I came up with this solution, however if an item exist it only update the quantity 2 times on 3rd try It creates new list item with same desc, price but 1 quantity.
Cursor c = db.rawQuery("SELECT code,desc,price FROM TbLPrice WHERE code =" + txtCode.getText().toString(), null); //search database
if (c.moveToFirst()){ //if successful
txtDesc.setText(c.getString(1)); //get desc
txtPrice.setText(c.getString(2)); // get price @ database
HashMap<String, String> map = new HashMap<String, String>();
map.put("desc", c.getString(1));
map.put("price", c.getString(2));
if(list.isEmpty()){
map.put("quantity","1");
list.add(map);
adapter.notifyDataSetChanged();
}
else{
if(list.contains(map)){
int loc = list.indexOf(map);
Object o = list.get(loc);
@SuppressWarnings("unchecked")
HashMap<String, String> map2 = (HashMap<String, String>)o;
String b = (String) map2.get("quantity");
int quantity = Integer.parseInt(b) + 1;
map2.put("quantity", Integer.toString(quantity));
adapter.notifyDataSetChanged();
}
else{
map.put("quantity","1");
list.add(map);
adapter.notifyDataSetChanged();
}