Identifying The Group That Has Been Clicked In An ExpandableListView
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.
private class expandableListLongClickListener implements AdapterView.OnItemLongClickListener {
public boolean onItemLongClick (AdapterView<?> p, View v, int pos, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Long Click Info");
String msg = "pos="+pos+" id="+id;
msg += "\ngroup=" + ExpandableListView.getPackedPositionGroup(id);
msg += "\nchild=" + ExpandableListView.getPackedPositionChild(id);
builder.setMessage(msg);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { }
} );
AlertDialog alert = builder.create();
alert.show();
return true;
}
}
Post a Comment for "Identifying The Group That Has Been Clicked In An ExpandableListView"