我正在使用ArrayList
with a HashMap
inside 来解析JSON
和扩展动态 UI。问题只是最后一个key
,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);
}
}
我的布局膨胀只有两个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
并且value
是Strings
在类的最顶部声明的。我认为这些实际上是在HashMap
. 因此,当我像这样把它们拉出来的时候
tv.setText(map.get(key));
Log.v("KEY", key);
tv1.setText(map.get(value));
Log.v("VALUE", value);
它们总是一样的。我对吗?如果是这样,我将如何解决这个问题?Strings
每次都简单地声明新的?