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 6962281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:38:25+00:00 2026-05-27T15:38:25+00:00

I current have an expandable list that I’d like to be able to search

  • 0

I current have an expandable list that I’d like to be able to search through. I’ve been following this guide on the android developer website but I’m stuck at the actual method and presenting results.

1) My search method is called doMySearch
2) I want to search through the children array

Code for expandable list
package com.sammy.umass;

import android.app.ExpandableListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;


public class DirectionsScreenActivity extends ExpandableListActivity {
ExpandableListAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.directions);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setDefaultKeyMode( DEFAULT_KEYS_SEARCH_LOCAL);
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction()))
    {
      String query = intent.getStringExtra(SearchManager.QUERY);
      doMySearch(query);
    }
    // Set up our adapter
    mAdapter = new MyExpandableListAdapter();
    setListAdapter(mAdapter);
    registerForContextMenu(getExpandableListView());
}

private void doMySearch(String query) 
{
    //Search Method

}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    menu.setHeaderTitle("Sample menu");
    menu.add(0, 0, 0, R.string.expandable_list_sample_action);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();

    String title = ((TextView) info.targetView).getText().toString();

    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 + ": Child " + childPos + " clicked in group " + groupPos,
                Toast.LENGTH_SHORT).show();
        return true;
    } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
        int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
        Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
        return true;
    }

    return false;
}

/**
 * A simple adapter which maintains an ArrayList of photo resource Ids. 
 * Each photo is displayed as an image. This adapter supports clearing the
 * list of photos and adding a new photo.
 *
 */
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private String[] groups = { "   Academic", "   Research & Labs", "   Dining Commons", "   Points of Interest","   Adminstriation",
            "   Central","   North Apartments","   Northeast","   Orchard Hill","   Southwest","   Sylvan"};
    private String[][] children = {
            //Academic Buildings
            { "Arnold House","Barlett Hall","Berkshire House","Blaisdell House","Bowditch Hall","Clark Hall","Draper Hall","Duda Hall","East Experiment Station",
                "Fernal Hall","French Hall","Furcolo Hall","H. Alfond Management Center","Hampshire House","Herter Hall","Hillis House",
                "Holdsworth Hall","Isenberg School Of Management","Machmer Hall","Marcus Hall","Marston Hall","Middlesex House","Montague House",
                "Munson Hall","New Africa House","Skinner Hall","South College","Stockbridge/Bowker","Studio Arts Building","Thompson Hall","Tobin Hall",
                "West Experiment Station"},
            //Research and Labs
            { "Agricultural Engineering","Apiary Laboratory","Astronomy Building","Chenoweth Laboratory","Computer Science Building",
            "Conte Polymer Research Building","Engineering Laboratory 1","Engineering Laboratory 2","Flint Laboratory","Goessmann Laboratory",
            "Gunness Laboratory","Hasbrouck Laboratory","Hatch Laboratory","Integrated Science Building","Knowles Engineering Research Building",
            "Lederle Graduate Reaserch Building","Morrill Science Center","Paige Laboratory","Thayer ANimal Disease Laboratory","W.E.B. Du Bois Library"},
            //Dining Commons
            { "Berkshire", "Franklin","Hampden","Hampshire","Worcester" },
            //Points of Interest
            { "Bowditch Lodge","Boyden Gym","Campus Center/Hotel","Curry Hicks","Durfee Conservatory","Farley Lodge","Fine Arts Center","Grinnell Arena",
                "Haigis Mall","Mahar Auditorium","McGuirk Alumni Stadium","Mullins Center","Observatory","Old Chapel","Recreation Center","Student Union",
                "Totman Gym","Vistor's Center"},
            //Administration
            { "Army ROTC Building", "Auxiliary Services Food Store","Campus Center Parking","Chancellor's House","Cold Storage Building",
                    "Police Department","Forest and Parks Building","Goodell","Physical Plant","PVTA Transit Facility","Research Administration",
                    "Shade Tree Lab","Textbook Annex","University Health Services","Whitmore Administration"},
            //Central
            { "Baker", "Brett","Butterfield","Chadbourne","Gorman","Greenough","Van Meter","Wheeler" },
            //North Apartments
            { "North A", "North B","North C","North D" },
            //Northeast
            { "Crabtree", "Dwight","Hamlin","Johnson","Knowlton","Leach","Lewis","Mary Lyon","Thatcher" },
            //Orchard Hill
            { "Dickinson", "Field","Grayson","Webster" },
            //Southwest
            { "Cance", "Coolidge","Crampton","Emerson","James","John Adams","John Quincy Admas","Kennedy","MacKimmie","Melville","Moore","Patteron",
                "Pierpont","Prince","Thoreau","Washington" },
            //Sylvan
            { "Brown", "Cashin","McNamara"},

    };

    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    public TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64);

        TextView textView = new TextView(DirectionsScreenActivity.this);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        textView.setPadding(36, 0, 0, 0);
        return textView;
    }

    public View getChildView(final int groupPosition,final int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getChild(groupPosition, childPosition).toString());
        textView.setOnClickListener(new View.OnClickListener(){
          public void onClick(View view){
            Uri uriToOpen = getUriForView(groupPosition, childPosition);
            Intent i = new Intent(Intent.ACTION_VIEW, uriToOpen);
            startActivity(i);
          }


    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    public int getGroupCount() {
        return groups.length;
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getGroup(groupPosition).toString());
        return textView;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }



}
}
  • 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-27T15:38:26+00:00Added an answer on May 27, 2026 at 3:38 pm

    First off, your post is very difficult to read because you’ve posted much more code than is necessary to answer the question.

    Anyway, the children array is declared in BaseExpandableListAdapter as a private member variable. You haven’t provided any facility to access the array from a different class (ie. DirectionsScreenActivity) so you need to provide some way for DirectionsScreenActivity to access the array before you can do anything with it.

    One way would be to make a public (or protected) method that simply returns the array, but that could provide too much access.

    What I would do is move the search method to MyExpandableListAdapter ie.

    public [some meaningful return type] doSearch(String query)
    {
          // Search code
    }
    

    Instead of calling doMySearch() in DirectionsScreenActivity — call doSearch() via your instance of MyExpandableListAdapter (since that already has your array), and do whatever you need to do with the result ex:

    if (Intent.ACTION_SEARCH.equals(intent.getAction()))
    {
      String query = intent.getStringExtra(SearchManager.QUERY);
      [some meaningful type] result = mAdapter.doSearch(query);
    
      // Do something with the result
    }
    

    As for how to do the search, that’s just standard Java and is not Android specific. There are a million and one examples of basic search algorithms in Java, so I won’t detail them here.

    As a side note, you can probably acheive better performance by using a Dictionary/Hashtable that hashes String --> List<String>. The keys would be the groups and the values would be the list of children.

    That way if someone is looking for all the admin buildings, you can grab the list of admin buildings in constant time (by simply retrieving the list [value] from the hashtable, using the the key “Administration”). Finding a building’s group given its name is a little trickier, and in the worst case would require iterating over all the items in the hashtable.

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

Sidebar

Related Questions

I current have some code that takes a screenshot and saves this to the
I current have the following regular expression to accept any numeric value that is
I current have the following XML fragment that I'm parsing to read its values
I current have this code that shows/hides content based on a select option. How
I have three current thoughts on how to do this: re-implement AuthenticationService, which uses
I have a current Date object that needs to be incremented by one day
I'm looking for the best method for the following issue I have. I current
Current I have this code: var imgCount = 36; var container = $('#3D-spin'); var
I current have an app that uses ALAsssetsLibrary to fetch the photos. I have
I current have the following attribute decorating one of the action method. [Authorize(Roles =

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.