baseAdapter的getCount()被多次调用

时间:2014-09-21 08:14:30

标签: android

我有一个扩展BaseAdapter的类,用于为抽屉内的listView的每一行插入一个图标和一个textView

public class NavRightDrawerListAdapter extends BaseAdapter {

private Context context;
LinkedList<String> userNameUsedForListView;
Map<String, Bitmap> urlUserImage;

public NavRightDrawerListAdapter(Context context, LinkedList<String> userNameUsedForListView, Map<String, Bitmap> returnBitMapFromURL) {
    this.context = context;
    this.userNameUsedForListView = userNameUsedForListView;
    this.urlUserImage = returnBitMapFromURL;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    int count = 0;
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.drawer_list_of_action, null);
    }

    ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);

    imgIcon.setImageBitmap(urlUserImage.get(userNameUsedForListView.get(count)));
    txtTitle.setText(userNameUsedForListView.get(count));
    count++;
    return convertView;
}

@Override
public int getCount() {
    return userNameUsedForListView.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

我的列表大小是1,getView被调用1次,通常,但getCount被调用六次? 为什么?是正常的行为?

我没有实施这个方法getItem()getItemId(),因为我不需要它们,我还是要实现它们吗? < / p>

非常感谢。

1 个答案:

答案 0 :(得分:1)

当AdapterView正在准备并布置其子视图时,可以调用适配器方法任意次数。所以,是的,这是正常的。

我无法找到明确说明getCount()是这种情况的来源,但是this answer Romain Guy注意到无法保证多少次{ {1}}将被调用。人们可以看到这种方法如何与getView()相互作用,因此,这种方法也需要被称为不确定的次数。

相关问题