1
我有一个ListView,它是基于所选RadioButton的值填充的,RadioButton的OnClick事件触发ListView以新值刷新。Android ListView不显示新项目
我有这个几乎工作,但我有的问题是,如果第一个RadioButton有6个项目要显示在ListView中,然后第二次点击RadioButton有10个项目来显示,ListView显示前6项10,然后再前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);
是否有重复列表项明显的原因是什么?
非常好,这是固定的。我仍然不明白为什么它首先这样做,但是这种方法解决了这个问题,而且效率更高,因此我不会再花时间去担心它。谢谢! – CrazyHorse
原因很明显,当getView方法被调用时,你正在膨胀你想要附加到listView的视图,但是你每次调用getView时都会膨胀视图。你从getView方法获得的convert视图并不总是为空,所以你需要在扩充一个新视图之前检查convert视图。 – osayilgan