0
在我的我的活動的onCreate方法:安卓:我ListAdapter重複循環
ListView listView = (ListView) findViewById(R.id.listView);
registerForContextMenu(listView);
listView.setItemsCanFocus(false);
Log.i(TAG, "list.size()" + list.size());
listView.setAdapter(new MyListAdapter(this, list));
listView.setTextFilterEnabled(true);
listView.setClickable(true);
MyListAdapter:
public class MyListAdapter extends BaseAdapter {
private static final String TAG = "MyListAdapter";
private Activity context;
protected LayoutInflater mInflater;
protected List<?> itemList;
public MyListAdapter(Activity context, List<Event> list) {
mInflater = LayoutInflater.from(context);
this.context = context;
itemList = list;
}
public View getView(int position, View convertView, ViewGroup parent) {
//DO Operations...
Event item = (Event)(itemList.get(position));
Log.i(TAG, "item.getName(): " + item.getName());
return convertView;
}
public int getCount() {
return itemList.size();
}
public Object getItem(int position) {
return itemList.get(position);
}
public long getItemId(int position) {
return position;
}
}
如果我有列表中的2項(A和B)我將有這個結果在logcat:
item.getName(): A
item.getName(): B
item.getName(): A
item.getName(): B
item.getName(): A
item.getName(): B
但是,項目正確顯示在listView上,沒有重複。 這是爲什麼?
convertView從不爲空,我仔細檢查。我的問題是,我在getView中有一些重複的邏輯... – virtual82