Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7564903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:00:41+00:00 2026-05-30T14:00:41+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:00:42+00:00Added an answer on May 30, 2026 at 2:00 pm

    Each item that you add to the ContextMenu should 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 in onContextItemSelected using the item id:

    switch (item.getItemId()){
        case UNIQUE_ID_1:
            //handle what to do
        break;
        case UNIQUE_ID_2:
            //handle what to do
        break;
        ....
    

    So you may need to change your implementation slightly. I would create a unique id as an int constant 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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this RelativeLayout I am using: <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:layout_width=match_parent
Blockquote I'm using ExpandableListView in android this is the code of getChildView in the
I'm trying to get something like this: http://img202.imageshack.us/img202/552/layoutoy.png . I'm using this as a
I'm currently using a CursorTreeAdapter to map a Curser to an ExpandableListView in Android.
Using the http://www.ifans.com/forums/showthread.php?t=132024 post from another question i am allowing the user to enter
I'm new to this ExpandableListView and I desperately need your help please. I'm using
Using LINQ on collections, what is the difference between the following lines of code?
using (var file_stream = File.Create(users.xml)) { var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(file_stream, this); file_stream.Close();
Using Flex 3, I would like to take an image snapshot such as this:
I'm using ExpandableListView with a SimpleCursorTreeAdapter. Is it possible to get the group number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.