Android: getView() called twice in custom adapter

前端 未结 4 1571
渐次进展
渐次进展 2021-02-13 04:53

I\'m setting a custom SimpleCursorAdapter to a ListView. For some reason FriendAdapter\'s getView() is called twice for every item in the DB. After some investigation (I have no

相关标签:
4条回答
  • 2021-02-13 05:19

    This is normal and can happen when you have a listview with height=wrap_content (among others):

    Look at the last post: http://groups.google.com/group/android-developers/browse_thread/thread/4c4aedde22fe4594

    0 讨论(0)
  • 2021-02-13 05:20

    I used this. The getView runs twice, but if you check if convertView is null, the code inside will be run once.

    public View getView(int position, View convertView, ViewGroup parent) {
        View superView = super.getView(position, convertView, parent);
        if (convertView == null)
        {
             // Customize superView here
    
        }
        return superView;
    }
    
    0 讨论(0)
  • 2021-02-13 05:27

    For me it seems like the view is created twice in the same method. One in "if(convertView==null)" and the other "else". If I did nothing in once of the if statements then it is only created once. It seems like the method itself is only called once though.

    0 讨论(0)
  • 2021-02-13 05:31

    In order to getView get call only once for each row, you need to call super.getView and then change the returned view. It's something like this

    public View getView(int position, View convertView, ViewGroup parent) {
        View superView = super.getView(position, convertView, parent);
        if (mCursor.moveToPosition(position)) {
             // Customize superView here
    
        }
        return superView;
    }
    
    0 讨论(0)
提交回复
热议问题