[英]Android ListView does not display new items
I have a ListView which is populated based on the value of the selected RadioButton, with the RadioButtons' OnClick events triggering the ListView to refresh with new values. 我有一个基于所选RadioButton的值填充的ListView,其中RadioButtons的OnClick事件触发ListView用新值刷新。
I have this almost working, but the issue I have is that if the first RadioButton has 6 items to display in the ListView, then the second clicked RadioButton has 10 items to display, the ListView displays the first 6 items of the 10, then the first 4 items again, as if it's wrapping the items. 我几乎可以正常工作了,但是我的问题是,如果第一个RadioButton有6个项目要显示在ListView中,那么第二个单击的RadioButton有10个项目要显示,ListView显示前10个项目中的前6个项目,然后头4个项目,好像包裹了这些项目。 I am populating the ListView using ArrayList<HashMap<String, String>> listValues
and a SimpleAdapter and I have checked that listValues does contain 10 distinct values. 我正在使用ArrayList<HashMap<String, String>> listValues
和SimpleAdapter填充ListView,并且我检查了listValues确实包含10个不同的值。
The code to display the items is as follows: 显示项目的代码如下:
ListView listview = (ListView)getView().findViewById(R.id.my_listview);
SimpleAdapter simpleAdapt =
new SimpleAdapter(getView().getContext(), listValues,
R.layout.my_listitem,
null, null) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
HashMap<String, String> listItem = listValues.get(position);
View v = convertView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.my_layout, null);
TextView nameTextview =
(TextView)v.findViewById(R.id.my_textview);
nameTextview.setText(listItem.get("name"));
TextView detailTextview =
(TextView)v.findViewById(R.id.my_textview_2);
detailTextview.setText(listItem.get("detail"));
if (listItem.get("main_image") != null) {
int imageId = Integer.valueOf(listItem.get("main_image"));
(ImageView)v.findViewById(R.id.my_imageview);
image.setImageResource(imageId);
}
}
return v;
};
};
listview.setAdapter(simpleAdapt);
Is there an obvious reason for the repeating list items? 重复列表项是否有明显的原因?
The way you are handling the getView method is not that efficient. 您处理getView方法的方式效率不高。 You should give a try to ViewHolder pattern. 您应该尝试使用ViewHolder模式。 The Link below is a good practice that you can follow. 下面的链接是您可以遵循的良好做法。
http://www.vogella.com/articles/AndroidListView/article.html http://www.vogella.com/articles/AndroidListView/article.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.