Skip to content Skip to sidebar Skip to footer

Identifying The Group That Has Been Clicked In An Expandablelistview

I'm trying to identify a view that has been clicked in an expandableListView. When I set an OnItemLongClickListener I get an argument that shows me the position of the clicked view

Solution 1:

No, the long parameter is not the packed value, this is the ID generated by your adapter (getCombinedChildId()). Attempting to interpret an ID, even if you generate it in a certain way would be a bad idea. Id is an id.

I believe the right way is to use ExpandableListView.getExpandableListPosition(flatPos) method. Your "pos" argument passed to the listener is, in fact, flat list position. getExpandableListPosition() method returns the packed position which can be then decoded into separate group and child positions using the static methods of ExpandableListView.

I have hit this problem myself today so I am describing the solution I have found working for me.

Solution 2:

The long id parameter passed by the onItemLongLongClick method is a packed value. You can retrieve group position with ExpandableListView.getPackedPositionGroup(id) Child position is obtained with ExpandableListView.getPackedPositionChild(id). If Child == -1 then the long click was on the group item.

Below is an example listener class demonstrating unpacking of id.

privateclassexpandableListLongClickListenerimplementsAdapterView.OnItemLongClickListener {
    publicbooleanonItemLongClick(AdapterView<?> p, View v, int pos, long id) {
        AlertDialog.Builderbuilder=newAlertDialog.Builder(ctx);
        builder.setTitle("Long Click Info");
        Stringmsg="pos="+pos+" id="+id;
        msg += "\ngroup=" + ExpandableListView.getPackedPositionGroup(id);
        msg += "\nchild=" + ExpandableListView.getPackedPositionChild(id);
        builder.setMessage(msg);
        builder.setPositiveButton("Ok", newDialogInterface.OnClickListener() {
            publicvoidonClick(DialogInterface dialog, int id) {   }
        } );
        AlertDialogalert= builder.create();
        alert.show();           
        returntrue;
    }
}

Post a Comment for "Identifying The Group That Has Been Clicked In An Expandablelistview"