android ListView简单的适配器,重复项目 [英] android ListView Simple Adapter, item repeating
问题描述
我创建了一个自定义的 ListView
,并在java中用 SimpleAdapter填充了
ListView
/ code>。这是我的代码:
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(歌曲[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);
现在我创建了两个字符串数组
Artist分别放入 HashMap
中,然后将 HashMap
放入 ArrayList
称为
itemList
。之后,我在参数中设置了默认列表项ID的适配器。
我遇到的问题是当我运行应用程序时,列表视图仅显示字符串数组的最后一项和子项重复。这是供参考的图片。
我的ListView
<我想知道为什么会发生这种情况。我尝试在()循环中将
itemList.add(itemMap);
放在之外,但它仍然不起作用。帮助将真正赞赏与一个适当的解释,因为我有点新的这一点。谢谢!解决方案您可以通过重新创建您的 itemMap
对象在每一次迭代。尝试以下方法。
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(歌曲[i] ++ artists [i]);
itemList.add(itemMap);
$ / code>
更改为
for(int i = 0; i< songs.length; i ++){
itemMap = new HashMap< String,String>();
itemMap.put(song,songs [i]);
itemMap.put(artist,artists [i]);
System.out.println(歌曲[i] ++ artists [i]);
itemList.add(itemMap);
解释:
请参阅此处。注意 HashMap
是一个键
值
对,这意味着它会为钥匙存储一些价值。所以 HashMap
将不允许重复键。从你的代码中,你已经添加了歌曲
和艺术家
的值。所以你不能再添加这些键的值。但是 itemlist
在每次迭代中添加 itemMap
。这就是重复相同记录的列表的原因。为了避免这个问题,只需重新初始化 itemMap
对象。
我希望这会对你有帮助。
I created a custom ListView
and in java filled that ListView
with a SimpleAdapter
. Here's my code:
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);
Now I created two String Arrays
Song and Artist respectively and put them in a HashMap
and then put the HashMap
in a ArrayList
called itemList
. After that i set up the adapter with the default list item id's in the parameter.
The problem I am facing is that when i run the application, the list view shows only the last item and subItem of the String Arrays repeatedly. Here's the image for reference.
My ListView
I'd like to know why this is happening. I tried putting the itemList.add(itemMap);
outside the for()
loop but it still didn't work. Help would really be appreciated with a proper explanation, cause i'm kinda new to this. Thank You!
解决方案 You can achieve this by recreating your itemMap
object at every iteration. Try the following way.
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);
}
Change this to
for (int i = 0; i < songs.length; i++) {
itemMap = new HashMap<String, String>();
itemMap.put("song", songs[i]);
itemMap.put("artist", artists[i]);
System.out.println(songs[i] + " " + artists[i]);
itemList.add(itemMap);
}
Explaination:
Refer here. Note HashMap
is a key
value
pair which means it will store some value for a key. So HashMap
will not allow duplicate key. From your code already youe have added the value for song
and artist
. So you can not add the value for these keys again. But the itemlist
add the itemMap
at every iteration. That is why the list repeating the same record. To avoid this problem simply re instantiate the itemMap
object.
I hope this will help you.
这篇关于android ListView简单的适配器,重复项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!