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 :)