custom listview adapter getView method being called multiple times, and in no coherent order

后端 未结 11 2111
情话喂你
情话喂你 2020-11-22 04:50

I have a custom list adapter:

class ResultsListAdapter extends ArrayAdapter {

in the overridden \'getView\' method I do a

相关标签:
11条回答
  • 2020-11-22 05:29

    Ques: Why Adapter calls getView() manytimes? Ans: As Listview renders on scrolling is refreshes it's view with next upcoming views, for which adapter needs to get views by calling getView().

    Ques: Why it's calls lesser if listview width and height set to fill_parent? Ans: Because as inflator has the fixed size for the screen area for list it calculates once for rendering the views onto the screen.

    Hope it will resolve your query.

    0 讨论(0)
  • 2020-11-22 05:29

    For all of you who still (After setting the height of the ListView to match_parent) are stuck (like I was):

    You also have to set the height of the parent layout to match_parent.

    See example below. The LinearLayout is the parent here:

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/something" />
    
            <ListView
                android:id="@+id/friendsList"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    
    0 讨论(0)
  • 2020-11-22 05:31

    I was having the same problem with the dropdown in an AutoCompleteTextView. I was fighting with the problem for two days until I arrive here and you show me the solution.

    If I write dropDownHeight="match_parent" the problem is fixed. Now the problem is related to UI (when you have one item the dropdown is too large) but the problem of multiple calls (much more important) is fixed.

    Thank you!!

    0 讨论(0)
  • 2020-11-22 05:33

    Try with match_parent on the layout_height property of the list view. It will prevent getView() to be called so often.

    0 讨论(0)
  • 2020-11-22 05:36

    This maybe coming late but if you are using layout_weight remember to always set layout_width="0dp"

    0 讨论(0)
  • 2020-11-22 05:37

    "Why is getview called for each row three times?" Because getView is called when you scroll on listview and to say better then that it is called when the position of an view of your list is changed!

    0 讨论(0)
提交回复
热议问题