Updating List
Solution 1:
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):
publicclassMyItem
{
String description;
float price;
int quantity;
publicvoidsetDescription(String description)
{
this.description = description;
}
publicvoidsetPrice(float price)
{
this.price = price;
}
publicvoidsetQuantity(int quantity)
{
this.quantity = quantity;
}
publicvoidincreaseQuantity()
{
this.quantity++;
}
@OverridepublicinthashCode()
{
finalintprime=31;
intresult=1;
result = prime * result + ((description == null) ? 0 : description.hashCode());
return result;
}
@Overridepublicbooleanequals(Object obj)
{
if (this == obj)
returntrue;
if (obj == null)
returnfalse;
if (getClass() != obj.getClass())
returnfalse;
MyItemother= (MyItem) obj;
if (description == null)
{
if (other.description != null)
returnfalse;
}
elseif (!description.equals(other.description))
returnfalse;
returntrue;
}
}
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:
publicvoidqueryForItem(String itemCode)
{
Cursor cursor = db.rawQuery("SELECT code,desc,price FROM TbLPrice WHERE code =" + itemCode, null);
if (cursor.moveToFirst())
{
processCursor(cursor);
}
cursor.close();
}
privatevoidprocessCursor(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 codeint 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 :)
Solution 2:
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 databaseif (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();
}
Post a Comment for "Updating List"