开发者

android: my ListAdapter duplicates loop

In my onCreate method of my Activity:

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;
    }

    }

If I have 2 items in the list (A and B) I'll have this result in the logcat开发者_如何学Go:

item.getName(): A
item.getName(): B
item.getName(): A
item.getName(): B
item.getName(): A
item.getName(): B

however, the items are correctly displayed on the listView, without duplications. Why is that?


You need to create a view if convertView is null (in getView()). Other than that, I would not worry about it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜