简体   繁体   English

哈希图未填充

[英]Hashmap not populating

I'm trying to populate Hashmap as following: 我正在尝试填充Hashmap ,如下所示:

public static final String KEY_PROPNAME = "";
public static final String KEY_KEYWORDS = "";
public static final String KEY_THUMB_URI = "";
ArrayList<HashMap<String, String>> clipsList = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < clipDetailsArr.length; i = i
            + constants.noOfColumns) {

        HashMap<String, String> map = new HashMap<String, String>();
        map.put(KEY_PROPNAME, clipDetailsArr[i]);
        map.put(KEY_PROPTYPE, clipDetailsArr[i + 1]);
        map.put(KEY_PRICE, clipDetailsArr[i + 2]);
        map.put(KEY_LOCATION, clipDetailsArr[i + 3]);
        map.put(KEY_SQFT, clipDetailsArr[i + 4]);
        map.put(KEY_SQFTTYPE, clipDetailsArr[i + 5]);
        map.put(KEY_BED, clipDetailsArr[i + 6]);
        map.put(KEY_BATH, clipDetailsArr[i + 7]);
        map.put(KEY_KEYWORDS, clipDetailsArr[i + 8]);
        map.put(KEY_THUMB_URI, clipDetailsArr[i + 9]);

        Log.i("Main", "Clip clipDetailsArr[]: 0=" + clipDetailsArr[i] + " ,1=" + clipDetailsArr[i + 1]
                + " ,2=" + clipDetailsArr[i + 2] + " ,3=" + clipDetailsArr[i + 3] + " ,4="
                + clipDetailsArr[i + 4] + " ,5=" + clipDetailsArr[i + 5] + " ,6="
                + clipDetailsArr[i + 6] + " ,7=" + clipDetailsArr[i + 7] + " ,8="
                + clipDetailsArr[i + 8] + " ,9=" + clipDetailsArr[i + 9]);

        Log.i("Main", "Clip PROPNAME=" + MainActivity.KEY_PROPNAME
                + " ,KEYWORDS" + MainActivity.KEY_KEYWORDS + " ,URI="
                + MainActivity.KEY_THUMB_URI);

        Log.i("Main", "Clip get PROPNAME=" + map.get(KEY_PROPNAME)
                + " ,KEYWORDS" + map.get(KEY_KEYWORDS) + " ,URI="
                + map.get(KEY_THUMB_URI));          
        clipsList.add(map);
    }

Output of log is something like this: 日志的输出是这样的:

Clip clipDetailsArr[]: 0=Opt out ,1=Residentail ,2=10000 ,3=Andheri ,4=500 ,5=Carpet ,6=2 ,7=1 ,8=optout ,9=/mnt/sdcard/Clipping/optout.png

Clip PROPNAME= ,KEYWORDS ,URI=

Clip get PROPNAME=/mnt/sdcard/Clipping/optout.png,
         KEYWORDS/mnt/sdcard/Clipping/optout.png,
         URI=/mnt/sdcard/Clipping/optout.png//AND SO ON

Referring to above log, I have values in my array and I'm putting them into array by using map.put(key,value) but in 2nd log MainActivity.KEY_PROPNAME and other fields are empty. 参考上面的日志,我在数组中有值,并通过使用map.put(key,value)将它们放入数组中map.put(key,value)但在第二个日志中, MainActivity.KEY_PROPNAME和其他字段为空。 Also, when I use map.get(key) , all the column have data of last column. 另外,当我使用map.get(key) ,所有列都有最后一列的数据。

Note that last column data is getting populated appropriately. 请注意,最后一列数据已正确填充。

Am I doing something wrong here? 我在这里做错什么了吗? Any help appreciated. 任何帮助表示赞赏。

You are using the same key "" for at least three .put() !! 您在至少三个.put()中使用相同的键"" You have defined : 您已定义:

public static final String KEY_PROPNAME = "";
public static final String KEY_KEYWORDS = "";
public static final String KEY_THUMB_URI = "";

And then you populate the Map as : 然后将Map填充为:

map.put(KEY_PROPNAME, clipDetailsArr[i]);
map.put(KEY_KEYWORDS, clipDetailsArr[i + 8]);
map.put(KEY_THUMB_URI, clipDetailsArr[i + 9]);

As per the Javadoc : 根据Javadoc

If the map previously contained a mapping for the key, the old value is replaced. 如果该映射先前包含该键的映射,则将替换旧值。

public static final String KEY_PROPNAME = "";
public static final String KEY_KEYWORDS = "";
public static final String KEY_THUMB_URI = "";

Here all of your keys have same value/name, Hence all the data will store in the same key !! 在这里,您所有的键都有相同的值/名称,因此所有数据都将存储在同一键中!

public static final String KEY_PROPNAME = "";
public static final String KEY_KEYWORDS = "";
public static final String KEY_THUMB_URI = "";

This is the error, instead of creating empty strings from your name you should use them as the Keys to get your requested behavior. 这是错误,而不是从您的名称创建空字符串,您应该将它们用作“键”以获取所需的行为。

Just to keep your code almost the same you could to something like: 只是为了使您的代码几乎相同,就可以执行以下操作:

public static final String KEY_PROPNAME = "KEY_PROPNAME";
public static final String KEY_KEYWORDS = "KEY_KEYWORDS";
public static final String KEY_THUMB_URI = "KEY_THUMB_URI";

That should to the trick even if it is a little funky ;) 即使有点时髦,这也应该是窍门;)

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

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