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

后端 未结 11 2112
情话喂你
情话喂你 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:39

    I am not able to answer your "Why" question but i definitely have a solution to the problem of the irritating "ListView items repeating" problem(if you have items in ur collection which are more than the screen height).

    As many people above have mentioned, keep the android:layout_height property of the ListVew tag as fill_parent.

    And about the getView() function, the solution is to use a static class called ViewHolder. Check out this example. It successfully does the task of adding all the items in ur Array or ArrayCollection.

    Hope this helps friends!!

    Best Regards, Siddhant

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

    I am having the same issue. If I have height set to fill_parent, then I get "usually" 2 calls per row. But, if I set height of my ListView to exact value, let's say to 300dp, then I get exactly one GetView call per row.

    So, it seems to me that the only way is to first determine height of the screen, then programmatically set height of listvilew to that value. I don't like it. I hope there is a better way.

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

    i made this solution, maybe is not the best one, but does the work...

    //initialize control string
    private String control_input = " ";
    

    then =

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        View gridview = convertView;
    
        // change input_array for the array you use
        if (!control_input.equals(input_array[position])) {
            control_input = input_array[position];
    
            //do your magic
    
        } 
    
        return gridview;
    }
    

    hope it helps!

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

    I got rid of this issue when I changed both layout_width and layout_height to match_parent (changing only layout_height didn't help).


    Helpful note watch out if you have nested items. You've got to change the "highest" one to match_parent. Hope it helps someone.

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

    This is not an issue, there is absolutely no guarantee on the order in which getView() will be called nor how many times. In your particular case you are doing the worst thing possible with a ListView by giving it a height=wrap_content. This forces ListView to measure a few children out of the adapter at layout time, to know how big it should be. This is what provides ListView with the convertViews you see passed to getView() even before you scroll.

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