【问题标题】:getView not called on baseAdapter class未在 baseAdapter 类上调用 getView
【发布时间】:2014-09-20 16:27:48
【问题描述】:

我有一个扩展BaseAdapter 的类,我用它来为抽屉内listView 的每一行插入一个icon 和一个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() {
        // TODO Auto-generated method stub
        return 0;
    }

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

在我的活动中,我这样做:

[...]
rightDrawerLinearLayout = (LinearLayout) findViewById(R.id.right_drawer_ll);
rightDrawerListForFollow = (ListView) findViewById(R.id.right_drawer);
NavRightDrawerListAdapter adapter = new NavRightDrawerListAdapter(getApplicationContext(), userNameUsedForListView,returnBitMapFromURL);
rightDrawerListForFollow.setAdapter(adapter);
[...]

我注意到 getView 没有被调用,有人可以解释一下为什么吗?

非常感谢。

【问题讨论】:

  • 我猜由于您的适配器声称为空(getCount 返回 0),因此 ListView 不会打扰请求项目视图
  • 好吧,我必须返回适配器使用的 lsit 的大小?我不能把它留到 0 吗?

标签: android listview navigation-drawer baseadapter


【解决方案1】:

在你的方法中

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

你返回 0。所以你的方法不会被调用。而是在 getCount 方法中返回列表视图的大小。

【讨论】:

  • 感谢您的回答,首先调用getCount 对吗?和 getView?
  • 是的,首先调用您的 getCount() 方法,然后调用 getView() 的次数与您返回的计数器值一样多。
【解决方案2】:

这里已经回答了:

Custom adapter getview is not called

您的 getCount 方法返回 0,因此适配器无需调用 getView()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    相关资源
    最近更新 更多