Hashmap只返回最后一个键值

时间:2013-06-28 16:06:33

标签: android

我正在使用内部有ArrayList的{​​{1}}来解析HashMap并为动态用户界面充气。问题是只插入了最后JSONkey。我无法弄清楚原因。

这是我在value

中插入值的方法
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); } } ,我的logcat显示以下内容

TextViews

最近怎么回事?我的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

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

map.put(key, value);

如果您注意到key = (String) iterator.next(); value = s.getString(key); keyvalue,则会在类的最顶层声明。我认为这些实际上是Strings内被覆盖的值。因此,当我像这样把它们拉出来时

HashMap

它们总是一样的。我对吗?如果是这样,我该如何解决?只需每次声明新的tv.setText(map.get(key)); Log.v("KEY", key); tv1.setText(map.get(value)); Log.v("VALUE", value);

3 个答案:

答案 0 :(得分:3)

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

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

除此之外,每次将数据添加到数组时都需要创建一个新地图,否则你只需要在agin上面添加相同的地图,每次调用{{ 1}},您可能会覆盖先前已添加到数组中的地图中的值。

map.put

然后当你再次把它们写出来时,你会从地图上读出它们:

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

答案 1 :(得分:2)

我认为你应该改变

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

 map.put(key, value);

HashMap拥有一对键和值。如果您使用相同的键插入多个项目,则只能找到最后一个

答案 2 :(得分:2)

不应该

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

    map.put(key, value);

然后使用类似:map.getKeys();的内容 仔细检查所有键并获取值?