私は驚くほどうまく動作しない次のコードを持っています。HashmapからArrayListへのループで正しい値が保持されていません。直し方?
needsInfoView = (ListView) findViewById(R.id.needsInfo);
needsInfoList = new ArrayList<>();
HashMap<String, String> needsInfoHashMap = new HashMap<>();
for (int i = 0; i < 11; i++) {
needsInfoHashMap.put("TA", needsTitleArray[i]);
needsInfoHashMap.put("IA", needsInfoArray[i]);
Log.e("NIMH",needsInfoHashMap.toString());
//Here, I get the perfect output - TA's value, then IA's value
needsInfoList.add(needsInfoHashMap);
Log.e("NIL",needsInfoList.toString());
//This is a mess - TA, IA values for 12 entries are all the same, they are the LAST entries of needsTitleArray and needsInfoArray on each ArrayList item.
needsInfoAdapter = new SimpleAdapter(getBaseContext(), needsInfoList,
R.layout.needsinfocontent, new String[]{ "TA", "IA"},
new int[]{R.id.ta, R.id.ia});
needsInfoView.setVerticalScrollBarEnabled(true);
needsInfoView.setAdapter(needsInfoAdapter);
}
ログラインの下のコメントを参照してください。それは私が受け取る出力を説明します。 ArrayListの値をSimpleAdapter経由でListViewの2つのテキストフィールドに渡すにはどうすればよいですか?
はあなたにあなたが各反復でMap
に入れエントリー前の反復で置いエントリを置き換えることを意味
List
に同じ
HashMap
インスタンスを複数回追加する
HashMapを試してみてくださいコード
以下のようなループの外で、あなたの
needsInfoView
listview
にごneedsInfoAdapter
を設定する必要がありますされ一意性のために設計されていて、以前に同じキーを追加しようとしている場合は、キー値を更新します –