0

我正在使用ArrayListwith a HashMapinside 来解析JSON和扩展动态 UI。问题只是最后一个keyvalue正在插入。我不知道为什么。

这就是我插入值的方式doInBackground()

for (JSONObject s : clientList) {
    Log.v("CHECKING S", s.toString());
    // Iterator containing all the keys
    Iterator<String> iterator = s.keys();
    while (iterator.hasNext()) {
        key = (String) iterator.next();
        value = s.getString(key);
        print("CHECKING VALUES 0: " + key);
        print("CHECKING VALUES 1: " + value);
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(key, value);            
        mList.add(map);
    }
}

当我检查它们的值时,它们都在那里,但是当我onPostExecute()像这样膨胀时,只有一个在那里。

protected void onPostExecute(String string) {
    // dismiss the dialog
    pDialog.dismiss();
    LinearLayout welcome = (LinearLayout) findViewById(R.id.cLLayout3);
    welcome.setVisibility(View.GONE);
    for (int i = 0; i < mList.size(); i++) {
        // get HashMap
        HashMap<String, String> map = mList.get(i);
        Log.v("RESPONSE", "Creating Client Assessment");
        // find
        LinearLayout content = (LinearLayout) findViewById(R.id.c_add);
        // create
        TextView tv = new TextView(Client.this);
        TextView tv1 = new TextView(Client.this);
        LinearLayout client1 = new LinearLayout(Client.this);
        // set
        tv.setLayoutParams(new LinearLayout.LayoutParams(0,
        LinearLayout.LayoutParams.MATCH_PARENT, 1f));
        tv1.setLayoutParams(new LinearLayout.LayoutParams(0,
        LinearLayout.LayoutParams.MATCH_PARENT, 1f));
        client1.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT));
        tv.setText(map.get(key));
        Log.v("KEY", key);
        tv1.setText(map.get(value));
        Log.v("VALUE", value);
        client1.setOrientation(LinearLayout.HORIZONTAL);
        // add
        client1.addView(tv);
        client1.addView(tv1);
        content.addView(client1);
    }
}

我的布局膨胀只有两个TextViews,我的 logcat 显示以下

06-28 12:05:06.024: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.024: V/KEY(15300): Tech Name
06-28 12:05:06.024: V/VALUE(15300):  Jon Doe
06-28 12:05:06.024: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.034: V/KEY(15300): Tech Name
06-28 12:05:06.034: V/VALUE(15300):  Jon Doe
06-28 12:05:06.034: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.034: V/KEY(15300): Tech Name
06-28 12:05:06.034: V/VALUE(15300):  Jon Doe
06-28 12:05:06.034: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.034: V/KEY(15300): Tech Name
06-28 12:05:06.034: V/VALUE(15300):  Jon Doe
06-28 12:05:06.034: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.044: V/KEY(15300): Tech Name
06-28 12:05:06.044: V/VALUE(15300):  Jon Doe
06-28 12:05:06.044: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.044: V/KEY(15300): Tech Name
06-28 12:05:06.044: V/VALUE(15300):  Jon Doe
06-28 12:05:06.044: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.044: V/KEY(15300): Tech Name
06-28 12:05:06.044: V/VALUE(15300):  Jon Doe
06-28 12:05:06.044: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.044: V/KEY(15300): Tech Name
06-28 12:05:06.044: V/VALUE(15300):  Jon Doe
06-28 12:05:06.044: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.044: V/KEY(15300): Tech Name
06-28 12:05:06.044: V/VALUE(15300):  Jon Doe
06-28 12:05:06.044: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.044: V/KEY(15300): Tech Name
06-28 12:05:06.044: V/VALUE(15300):  Jon Doe
06-28 12:05:06.044: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.044: V/KEY(15300): Tech Name
06-28 12:05:06.044: V/VALUE(15300):  Jon Doe
06-28 12:05:06.054: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.054: V/KEY(15300): Tech Name
06-28 12:05:06.054: V/VALUE(15300):  Jon Doe
06-28 12:05:06.054: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.054: V/KEY(15300): Tech Name
06-28 12:05:06.054: V/VALUE(15300):  Jon Doe
06-28 12:05:06.054: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.054: V/KEY(15300): Tech Name
06-28 12:05:06.054: V/VALUE(15300):  Jon Doe
06-28 12:05:06.054: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.054: V/KEY(15300): Tech Name
06-28 12:05:06.054: V/VALUE(15300):  Jon Doe
06-28 12:05:06.054: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.054: V/KEY(15300): Tech Name
06-28 12:05:06.054: V/VALUE(15300):  Jon Doe
06-28 12:05:06.054: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.054: V/KEY(15300): Tech Name
06-28 12:05:06.054: V/VALUE(15300):  Jon Doe
06-28 12:05:06.054: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.064: V/KEY(15300): Tech Name
06-28 12:05:06.064: V/VALUE(15300):  Jon Doe
06-28 12:05:06.064: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.064: V/KEY(15300): Tech Name
06-28 12:05:06.064: V/VALUE(15300):  Jon Doe
06-28 12:05:06.064: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.064: V/KEY(15300): Tech Name
06-28 12:05:06.064: V/VALUE(15300):  Jon Doe
06-28 12:05:06.064: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.064: V/KEY(15300): Tech Name
06-28 12:05:06.064: V/VALUE(15300):  Jon Doe
06-28 12:05:06.064: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.064: V/KEY(15300): Tech Name
06-28 12:05:06.064: V/VALUE(15300):  Jon Doe
06-28 12:05:06.064: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.064: V/KEY(15300): Tech Name
06-28 12:05:06.064: V/VALUE(15300):  Jon Doe
06-28 12:05:06.064: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.074: V/KEY(15300): Tech Name
06-28 12:05:06.074: V/VALUE(15300):  Jon Doe
06-28 12:05:06.074: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.074: V/KEY(15300): Tech Name
06-28 12:05:06.074: V/VALUE(15300):  Jon Doe
06-28 12:05:06.074: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.074: V/KEY(15300): Tech Name
06-28 12:05:06.074: V/VALUE(15300):  Jon Doe
06-28 12:05:06.074: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.074: V/KEY(15300): Tech Name
06-28 12:05:06.074: V/VALUE(15300):  Jon Doe
06-28 12:05:06.074: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.074: V/KEY(15300): Tech Name
06-28 12:05:06.074: V/VALUE(15300):  Jon Doe
06-28 12:05:06.074: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.084: V/KEY(15300): Tech Name
06-28 12:05:06.084: V/VALUE(15300):  Jon Doe
06-28 12:05:06.084: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.084: V/KEY(15300): Tech Name
06-28 12:05:06.084: V/VALUE(15300):  Jon Doe
06-28 12:05:06.084: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.084: V/KEY(15300): Tech Name
06-28 12:05:06.084: V/VALUE(15300):  Jon Doe
06-28 12:05:06.084: V/RESPONSE(15300): Creating Client Assessment
06-28 12:05:06.084: V/KEY(15300): Tech Name
06-28 12:05:06.084: V/VALUE(15300):  Jon Doe

发生什么了?我在哪里HashMap被覆盖?

编辑

我看到了关于将代码更改为此的答案

map.put(key, value);

如果我将代码更改为上述代码,那么它的 logcat 结果几乎是相同的。因此,您可能是对的,但结果仍然相同,哈希图正在被覆盖。我认为这里的实际错误在这些线上

key = (String) iterator.next();
value = s.getString(key);

如果您注意到key并且valueStrings在类的最顶部声明​​的。我认为这些实际上是在HashMap. 因此,当我像这样把它们拉出来的时候

tv.setText(map.get(key));
Log.v("KEY", key);
tv1.setText(map.get(value));
Log.v("VALUE", value);

它们总是一样的。我对吗?如果是这样,我将如何解决这个问题?Strings每次都简单地声明新的?

4

3 回答 3

3

如果键和值是需要存储在地图中的两个不同字段,那么我认为您的意思是这样的:

    map.put("key", key);
    map.put("value", value);

但是,除此之外,每次将其添加到数组时都需要创建一个新映射,否则您将一遍又一遍地添加相同的映射,并且每次调用时map.put,您都可能覆盖您已经添加到数组中的先前映射中的值。

    HashMap<String,String> map = new HashMap<String,String>();
    map.put("key", key);
    map.put("value", value);
    mList.add(map);

然后当你再次写出它们时,你会像这样从地图中读取它们:

    tv.setText(map.get("key"));
    tv1.setText(map.get("value"));
于 2013-06-28T16:23:16.820 回答
2

我觉得你应该改变

 map.put(key, value);
 map.put(value, value);

 map.put(key, value);

HashMap 包含一对键和值。如果你用同一个键插入多个项目,你会发现只有最后一个

于 2013-06-28T16:11:00.673 回答
2

不应该

    map.put(key, key);
    map.put(value, value);

    map.put(key, value);

然后使用类似 : map.getKeys(); 的东西遍历所有键并获取值?

于 2013-06-28T16:12:04.017 回答