I am using an ExpandableListView, same way they do in this sample code:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html
The ExpandableListView gets populated with categories and their subcategories (once I click on a category). Example:
-Dairy (category)
-Milk (sub category)
-cheese (sub category)
When I long-click on milk or cheese, a menu pops up using this function:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
ExpandableListContextMenuInfo info =(ExpandableListContextMenuInfo) menuInfo;
String selectedWord = ((TextView) info.targetView).getText().toString();
menu.setHeaderTitle(selectedWord.split(",")[1]); //set header
String itemId = selectedWord.split(",")[0];
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("action", "getProducts"));
nameValuePairs.add(new BasicNameValuePair("subcat_id", itemId));
String response = helper.makeHttpRequest(nameValuePairs);
String[] items = response.split(";");
for (int i=0; i<items.length; i++){
menu.add(0, 0, 0, items[i]);
}
}
Then, when I click on one of the items in the menu that pops up, I want to know which item in the list was selected (If I click on ‘milk’. for example, the menu has “1% milk”, “2% milk” etc._
This function gets fired:
@Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
String title = ((TextView) info.targetView).getText().toString();
String selected="";
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title + " selected: " + selected+ " " + childPos + " clicked in group " + groupPos , Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + " selected: " + selected + " " + childPos + " clicked in group " + groupPos , Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
But I don’t find a way to determine which item exactly in the list was clicked.
Any ideas?
Thank you in advance! Please let me know if I need to be more clear.
Each item that you add to the
ContextMenushould have its own unique identifier. This is useful for when you need to figure out which menu item was selected. So adding menu items needs to look like this:menu.add(0, unique_id, 0, items_name);. Then when you want to determine which item was selected you do something like this inonContextItemSelectedusing the item id:So you may need to change your implementation slightly. I would create a unique id as an
intconstant for each possible case and add each one specifically to the menu. It may a little more code but it will be so much easier to handle.Also just a suggestion since you are new here: when someone provides a good answer, you should accept it by clicking the check mark next to the answer. Good luck!