私はカスタムを作成しListView
、JavaでそれListView
をSimpleAdapter
. これが私のコードです:
setContentView(R.layout.activity_my);
list = (ListView) findViewById(R.id.lvList);
itemList = new ArrayList<HashMap<String, String>>();
itemMap = new HashMap<String, String>();
for (int i = 0; i < songs.length; i++) {
itemMap.put("song", songs[i]);
itemMap.put("artist", artists[i]);
System.out.println(songs[i] + " " + artists[i]);
itemList.add(itemMap);
}
SimpleAdapter adapter = new SimpleAdapter(this, itemList,
android.R.layout.simple_list_item_2, new String[] {"song",
"artist"}, new int[] { android.R.id.text1,
android.R.id.text2 });
list.setAdapter(adapter);
ここで、2 つのString Arrays
Song と Artist をそれぞれ作成し、それらを に配置してから、と呼ばれるHashMap
に配置しHashMap
ました。その後、パラメーターにデフォルトのリスト項目 ID を使用してアダプターをセットアップしました。ArrayList
itemList
私が直面している問題は、アプリケーションを実行すると、リスト ビューに文字列配列の最後のアイテムとサブアイテムのみが繰り返し表示されることです。参考までに画像はこちら。 マイリストビュー
なぜこれが起こっているのか知りたいです。itemList.add(itemMap);
ループの外側に配置しようとしましfor()
たが、それでも機能しませんでした。私はこれにちょっと慣れていないので、適切な説明で助けていただければ幸いです。ありがとう!