我有一个 ListView,它根据所选 RadioButton 的值填充,RadioButton 的 OnClick 事件触发 ListView 使用新值刷新。
我几乎可以正常工作,但我遇到的问题是,如果第一个 RadioButton 有 6 个项目要显示在 ListView 中,那么第二个单击的 RadioButton 有 10 个项目要显示,ListView 显示 10 个中的前 6 个项目,然后又是前 4 个项目,就好像它在包装项目一样。我正在使用 ArrayList<HashMap<String, String>> listValues
填充 ListView和一个 SimpleAdapter,我检查过 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
关于java - Android ListView 不显示新项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19640006/