正在更新列表<hashmap> </hashmap>

时间:2010-10-10 12:29:34

标签: android list hashmap

我只想知道如何,如果Hashmap已经在列表中添加1到数量,如果不是,则将其添加到列表。这就是我已经完成的事情,只要项目已在列表中即可添加。

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();

2 个答案:

答案 0 :(得分:0)

我提出了这个解决方案,但是如果一个项目存在它只在第3次尝试更新数量2次它创建具有相同desc,价格但是1个数量的新列表项。

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();
                    }

答案 1 :(得分:0)

从你上次的评论中,我想我知道你要做的是什么。首先,我会创建一个包含有关项目信息的小类,使其更容易使用(我只实现了必要的setter,你可能也想要一些getter(和其他函数)):

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;
    }
}

我实现了equals方法(通常也会实现hash方法),以便能够轻松检查列表中是否存在项目(为简单起见,我假设description唯一标识一个项目,您应该根据需要更改它。然后,您可以继续这样处理:

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();
}

我没有以任何方式对此进行过测试,但我确实应该做你想做的事情。希望你能够看到其中的逻辑:)