android ListView mListView.getChildAt(i) is null, how to solve it?

前端 未结 2 1369
时光取名叫无心
时光取名叫无心 2020-12-06 20:33

I create the below code:

 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(Adap         


        
相关标签:
2条回答
  • 2020-12-06 20:44

    you can not implement this in onItemClick.

    As you can access only visible child not all child.

    What you can do is on onItemClick

    you can send the position in adapter

    and then set the logic there in getView too change view

    and update the adapter in listview, or notify for changes.

    0 讨论(0)
  • 2020-12-06 21:00

    AdapterView.getCount() returns the number of data items, which may be larger than the number of visible views, that's why you are getting null pointer exception, because you are trying to find views which do not exist in the current visible ListView items.

    To solve this issue you will first need to find the first visible item in the ListView using getFirstVisiblePosition() and the last visible item using getLastVisiblePosition(). Change the for loop condition as:

    int num_of_visible_view=mListView.getLastVisiblePosition() - 
                                       mListView.getFirstVisiblePosition();
    
    for (int i = 0; i < num_of_visible_view; i++) {
          // do your code here
     }
    
    0 讨论(0)
提交回复
热议问题