Skip to content Skip to sidebar Skip to footer

How To Select Child In Expandablelistview?

I already asked the question but with no success. So I'm asking again (sorry btw). I still have this issue : How to access items inside ExpandableListView?. Let me resume. I have

Solution 1:

First off, the ExpandableListView supports an easy way to expand the group you need:

mGattServicesList.expandGroup(groupPosition);

To programmatically click an item is a little tricky. You're on the right track with using the performItemClick() method but you are a little off on how to use it. I will assume you are not using headers. That further complicates things.

First you need to obtain the View to be clicked. Oddly enough, this is not a requirement. You can safely invoke performItemClick() with a null View. The only downside being is that your child click listener will also receive a null view.

//First we need to pack the child's two position identifierslongpackedPos= ExpandableListView.getPackedPositionForChild(int groupPosition, int childPosition);

//Then we convert to a flat position to use with certain ListView methodsintflatPos= mGattServicesList.getFlatListPosition(packedPos);

//Now adjust the position based on how far the user has scrolled the list.intadjustedPos= flatPos - mGattServicesList.getFirstVisiblePosition();

//If all is well, the adjustedPos should never be < 0ViewchildToClick= mGattServicesList.getChildAt(adjustedPos);

Now we need the position and id to feed to performItemclick(). You'll see the steps are similar to retrieving the View. So really, you don't have to further type this out again...but to show what you need:

//You can just reuse the same variables used above to find the Viewlong packedPos = ExpandableListView.getPackedPositionForChild(int groupPosition, int childPosition);
int flatPos = mGattServicesList.getFlatListPosition(packedPos);

//Getting the ID for our childlongid = mGattServicesList.getExpandableListAdapter().getChildId(groupPosition, childPosition);

Finally, you can invoke your performItemClick():

performItemClick(childToClick, flatPos, id);

I should preface, I didn't get to check this code against an IDE so there may be some syntax errors that'll prevent compilation. But all in all should convey the, unfortunately, not so easy steps in programmatically clicking a child view.

Final note, the pic you provided shows the group and child counts start at 1. Be aware that they are actually considered zero based positions. So the first group is at position 0 and the first child for each group is at position 0.

Solution 2:

You can't have a listener for a specific item in a list, but you can do what you want. Just check to see if groupPosition and childPosition are the ones you want, then do the action (or some other action appropriate to the other items)

In the ExpandableListView.OnChildClickListener

@OverridepublicbooleanonChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    //groupPosition tells you what group the clicked child came from//childPosition tells you what child was clickedif (groupPosition == 2 && childPosition == 2) {
        //so this code only executes if the 2nd child in the 2nd group is clicked 
    }
    //you can ignore the other items or do something else when they are clicked
}

Post a Comment for "How To Select Child In Expandablelistview?"