Android custom ArrayAdapter getView method called multiple times - resetting dynamic TextView value

前端 未结 2 642
孤城傲影
孤城傲影 2021-01-13 11:22

The getView method in my custom ArrayAdapter is getting called multiple times, which I assume it is meant to. Problem is, I have a quantity TextView which is dynamically set

相关标签:
2条回答
  • 2021-01-13 11:38

    getView() will be called multiple times as you note. it shouldn't matter, because your array adapter is based on the state of it's internal data model (array, list of objects, whatever). getView() should be idempotent in that calling it multiple times shouldn't change the result.

    you say "when you scroll and the box goes off screen the value disappears". note sure what mean. when you scroll one of the views generated in getView() off the visible area, and when you scroll it back, the value is different? without any other information, i'd have to say that's not possible. the reason is again that unless you are modifying the internal state of the adapter, or changing the adapter, you will always generate the same view for a given position.

    by the way, convertView can be null, so you want to do something like,

    View v = convertView;
    if (v == null) {
      v = inflater.inflate(...);
    }
    
    // now use v in the rest of the method
    
    0 讨论(0)
  • 2021-01-13 11:50

    In your listview set android:height = "match_parent". Now getview called your dataset count. we can reuse the convertview. Check the convertview if it is NULL inflate your view. Otherwise, write your remaining code.

    if(convertView ! = null)
    {
     //rest of your Code
    }
    else
    {
    //inflate that view
    }
    
    0 讨论(0)
提交回复
热议问题