5

I create the below code:

 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {

            for (int i = 0; i < mListView.getCount(); i++) {
                View callLogView = mListView.getChildAt(i);   
                mRelativeLayout = (LinearLayout)callLogView.findViewById(R.id.myShow);
                if(i == position){
                    if(mRelativeLayout.getVisibility() == View.GONE){
                        mRelativeLayout.setVisibility(View.VISIBLE);
                    }
                    else{
                        mRelativeLayout.setVisibility(View.GONE);
                    }
                }else{
                    mRelativeLayout.setVisibility(View.GONE);
                }
            }

        }
    });

I want to realize a function like when i click one item of Listview, it will show a view, and the other items of Listview will be hidden. But mListView.getChildAt(i) will have the null pointer after exceed mListView.getChildCount().

How to solve this? Thanks in advance!

Miral Dhokiya
  • 1,720
  • 13
  • 26
DaringLi
  • 409
  • 2
  • 6
  • 16
  • possible duplicate of [ListView getChildAt returning null for visible children](http://stackoverflow.com/questions/6766625/listview-getchildat-returning-null-for-visible-children) – rds Oct 06 '13 at 17:10
  • possible duplicate of [Android: Access child views from a ListView](http://stackoverflow.com/questions/257514/android-access-child-views-from-a-listview) –  Aug 14 '14 at 01:12

2 Answers2

5

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
 }
Juan José Melero Gómez
  • 2,742
  • 2
  • 19
  • 36
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

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.

Nimish Choudhary
  • 2,048
  • 18
  • 17
  • Thanks for your answer. How to access all the sub-classes? Where can i realize? – DaringLi Feb 01 '13 at 06:15
  • I used this to keep the status of checkboxes in different view instances. I retrieved the status from an array of booleans in the activity then passed the array to an overridden constructor in the adapter, which I then used to update the CheckedTextViews in my ListView. When I tried to directly retrieve and update the CheckedTextViews from the activity, they were showing as null. – JanB Nov 20 '14 at 13:52