简体   繁体   English

哈希图并从中检索数据

[英]hashmap and retrieve data from it

I've to do an action on more item of my listview so i create an HashMap with the name of selected application (in my list) and relative position. 我必须对列表视图的更多项进行操作,因此我创建了一个HashMap,其中包含选定应用程序的名称(在列表中)和相对位置。

public void createHashMap(String name, int mPosition) {
    // TODO Auto-generated method stub
    mHashMap = new HashMap<String, Integer>();
    mHashMap.put(name, mPosition);
    Toast.makeText(context, name+": "+mPosition, Toast.LENGTH_LONG).show();

}

in onItemClick of my activity class 在我的活动类别的onItemClick中

        adapter.createHashMap(itemName, mPosition);

So, when i click on a row i show the toast with the correct information now i've need to retrieve the data into hashmap and i wrote this 因此,当我单击一行时,现在我将向烤面包展示正确的信息,我需要将数据检索到哈希图中,并且我将其编写为

public void retrieveHashMap() {
    Iterator myVeryOwnIterator = mHashMap.keySet().iterator();
    while(myVeryOwnIterator.hasNext()) {
        String key=(String)myVeryOwnIterator.next();
        int value= mHashMap.get(key);
        Toast.makeText(context, "Key: "+key+" Value: "+value, Toast.LENGTH_LONG).show();
    }

}

This work but when i call retrieveHashMap method i show ONLY the last insert value. 这项工作,但是当我调用retrieveHashMap方法时,我仅显示最后一个插入值。 How can i show ALL the inserted value?? 如何显示所有插入的值?

Move Toast.makeText outside the while loop and create the message you want inside the loop using the keys you iterate over. Toast.makeText while循环外,然后使用迭代的键在循环内创建所需的消息。

You also need to check if you override existing mHashMap in createHashMap() 您还需要检查是否在createHashMap()覆盖了现有的mHashMap

Look like your problem is that you initialize new hash map every time. 看起来您的问题是您每次都初始化新的哈希映射。 That's why you got only the last value. 这就是为什么只有最后一个值。 Try this: 尝试这个:

 public void createHashMap(String name, int mPosition) {
// init it only one time.
if(mHashMap  == null){
    mHashMap = new HashMap<String, Integer>();
}
mHashMap.put(name, mPosition);
Toast.makeText(context, name+": "+mPosition, Toast.LENGTH_LONG).show();

} }

And the toast has a duration time, so you shouldn't call it many times. 吐司有一个持续时间,所以您不应该多次称呼它。 If you try write log, you should use logcat. 如果尝试写日志,则应使用logcat。

Update: another reason, maybe your data has duplicate the name value. 更新:另一个原因,也许您的数据具有重复的名称值。 Try to debug or write to logcat and see if you have insert many name data have the same value. 尝试调试或写入logcat,看看是否插入了许多具有相同值的name数据。 Because the hash map is not accepted the duplicate key. 因为不接受哈希映射,所以重复密钥。 For example: 例如:

  mHashMap.put("test", 2);
  mHashMap.put("test", 3); // it will replace the item ["test" - 2] by ["test" - 3]. That's why you got only the last one. 

And make sure your iterator should look like this: 并确保您的迭代器应如下所示:

 Iterator<String> myVeryOwnIterator = mHashMap.keySet().iterator();
    while(myVeryOwnIterator.hasNext()) {
        String key=(String)myVeryOwnIterator.next();
        int value= mHashMap.get(key);
        System.out.println(key + " " + value);
    }

Hope this help! 希望有帮助!

When you use this method 使用此方法时

public void createHashMap(String name, int mPosition) {
// TODO Auto-generated method stub
mHashMap = new HashMap<String, Integer>();
mHashMap.put(name, mPosition);
Toast.makeText(context, name+": "+mPosition, Toast.LENGTH_LONG).show();

} }

you have to save hashmap or toast instance. 您必须保存哈希图或Toast实例。 when call 通话时

 mHashMap = new HashMap<String, Integer>();

and call 并打电话

Toast.makeText(context, name+": "+mPosition, Toast.LENGTH_LONG).show();

refreshed(create again) all data in instance. 刷新(再次创建)实例中的所有数据。

It looks like you are reseting mHashMap every time the method createHashMap is called: mHashMap = new HashMap(); 似乎您每次调用createHashMap方法时都在重置mHashMap:mHashMap = new HashMap();

Instead, you should have this only where you declare mHashMap (as a class member) and then in createHashMap only have the "mHashMap.put(name, mPosition)" part. 相反,仅在声明mHashMap(作为类成员)的地方才应具有此属性,然后在createHashMap中仅具有“ mHashMap.put(name,mPosition)”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM