[英]Android ListView does not display new items
我有一个基于所选RadioButton的值填充的ListView,其中RadioButtons的OnClick事件触发ListView用新值刷新。
我几乎可以正常工作了,但是我的问题是,如果第一个RadioButton有6个项目要显示在ListView中,那么第二个单击的RadioButton有10个项目要显示,ListView显示前10个项目中的前6个项目,然后头4个项目,好像包裹了这些项目。 我正在使用ArrayList<HashMap<String, String>> listValues
和SimpleAdapter填充ListView,并且我检查了listValues确实包含10个不同的值。
显示项目的代码如下:
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);
重复列表项是否有明显的原因?
您处理getView方法的方式效率不高。 您应该尝试使用ViewHolder模式。 下面的链接是您可以遵循的良好做法。
http://www.vogella.com/articles/AndroidListView/article.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.