更新列表<&HashMap的GT; [英] Updating List <HashMap>

查看:112
本文介绍了更新列表<&HashMap的GT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何,如果一个HashMap已经在名单上加1数量,如果它不然后将其添加到列表中。这就是我所做的,只是添加eventhough的项目已经是名单。

 列表=新的ArrayList<&HashMap的LT;字符串,字符串>>();
光标C = db.rawQuery(选择code,递减,价格从TbLPrice其中code =+ TXT code.getText()的toString(),NULL); //搜索数据库
                如果(c.moveToFirst()){//如果成功
                    txtDesc.setText(c.getString(1)); //获取降序
                    txtPrice.setText(c.getString(2)); //获取价格@数据库
                    HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                    map.put(DESC,c.getString(1));
                    map.put(价格,c.getString(2));
                    map.put(量,1);
                    list.add(地图);
                    adapter.notifyDataSetChanged();


解决方案

这是你最后的评论,我想我看到你想要做什么。首先,我想创建一个小型类持有有关项目的信息,使之更容易一点一起工作(我只实施了必要的制定者,你可能会想一些干将(和其他功能),以及):

 公共类MyItem
{
    字符串描述;
    浮动价格;
    INT数量;    公共无效setDescription(字符串描述)
    {
        this.description =描述;
    }    公共无效setPrice(浮动价)
    {
        this.price =价格;
    }    公共无效setQuantity(INT数量)
    {
        this.quantity =数量;
    }    公共无效increaseQuantity()
    {
        this.quantity ++;
    }    @覆盖
    公众诠释哈希code()
    {
        最终诠释黄金= 31;
        INT结果= 1;
        结果=黄金*结果+((介绍== NULL)?0:description.hash code());
        返回结果;
    }    @覆盖
    公共布尔等于(obj对象)
    {
        如果(这种== OBJ)
            返回true;
        如果(OBJ == NULL)
            返回false;
        如果(的getClass()!= obj.getClass())
            返回false;
        MyItem其他=(MyItem)目标文件;
        如果(介绍== NULL)
        {
            如果(other.description!= NULL)
                返回false;
        }
        否则,如果(!description.equals(other.description))
            返回false;
        返回true;
    }
}

我有落实等于方法(它是常见的,然后还实施法)轻松能够检查是否在列表中存在的项目(为简单起见,我假定说明唯一标识一个项目,你应该改变这种需要)。然后,您可以继续使用您的处理是这样的:

 公共无效queryForItem(字符串项code)
{
    光标光标= db.rawQuery(选择code,递减,价格从TbLPrice其中code =+项目code,NULL);
    如果(cursor.moveToFirst())
    {
        processCursor(光标);
    }
    cursor.close();
}私人无效processCursor(光标C)
{
    MyItem的newitem =新MyItem();
    newItem.setDescription(c.getString(1));
    newItem.setPrice(c.getFloat(2));
    newItem.setQuantity(1);    //假设项目(ArrayList的< MyItem>)的定义和先前在code初始化
    INT existingItemIndex = items.indexOf(的newitem);
    如果(existingItemIndex> = 0)
    {
        items.get(existingItemIndex).increaseQuantity();
    }
    其他
    {
        items.add(的newitem);
    }
    adapter.notifyDataSetChanged();
}

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

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

这篇关于更新列表&LT;&HashMap的GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆