[英]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.