Android: getView() called twice in custom adapter

偶尔善良 提交于 2019-12-03 10:44:17

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

user2064635

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

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.

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!