ExpandableListView Tutorial

So I struggled with this topic for about a day and a half and I just couldn’t find a decent tutorial for just using the SimpleExpandableListAdapter. There always seemed to be a requirement to make an adapter that extended baseAdapter. All I wanted to do was to pull in some parent groups and then have some child groups underneath those parents groups. This is what I came up with:


<strong>import</strong> java.util.ArrayList;

<strong>import</strong> java.util.HashMap;

<strong>import</strong> java.util.List;

<strong>import</strong> android.app.ExpandableListActivity;

<strong>import</strong> android.content.Intent;

<strong>import</strong> android.os.Bundle;

<strong>import</strong> android.view.ContextMenu;

<strong>import</strong> android.view.ContextMenu.ContextMenuInfo;

<strong>import</strong> android.view.Menu;

<strong>import</strong> android.view.MenuInflater;

<strong>import</strong> android.view.MenuItem;

<strong>import</strong> android.view.View;

<strong>import</strong> android.widget.SimpleExpandableListAdapter;

<strong>public</strong> <strong>class</strong> ReminderListActivity <strong>extends</strong> ExpandableListActivity{

      <strong>private</strong> <strong>static</strong> <strong>final</strong> <strong>int</strong> <em>ACTIVITY_CREATE</em>=0;

      <strong>static</strong> <strong>final</strong> String[] <em>tasks</em> = {

            "Finish writing to do list program",

            "Brooks Trap Mills",

            "FaceBook Application"

      };

      <strong>static</strong> <strong>final</strong> String[][] <em>taskAdds</em> ={

            {"Learn more about database entries", "Design Reminder Service", "Read Android in Action", "Finish reading Android for Dummies"},

            {"Read through the WordPress Code", "Contact Julie and Stephen about new website", "Read WordPress 3"},

            {"Finish reading the fbRocket API"}

      };

      <strong>private</strong> SimpleExpandableListAdapter mAdapter;

      @Override

      <strong>public</strong> <strong>void</strong> onCreate(Bundle savedInstanceState){

            <strong>super</strong>.onCreate(savedInstanceState);

            setContentView(R.layout.<em>reminder_list</em>);

            //Set up our adapter

            mAdapter = <strong>new</strong> SimpleExpandableListAdapter(<strong>this</strong>,

                        <span style="text-decoration: underline;">createGroupList()</span>,

                        R.layout.<em>group_row</em>,

                        <strong>new</strong> String[] {"taskName"},

                        <strong>new</strong> <strong>int</strong>[] {R.id.<em>groupname</em>},

                        <span style="text-decoration: underline;">createChildList()</span>,

                        R.layout.<em>child_row</em>,

                        <strong>new</strong> String[] { "taskAddName" },

                        <strong>new</strong> <strong>int</strong>[] {R.id.<em>childname</em>} );

      setListAdapter(mAdapter);

      }

      /**

        * Creates the group list out of the tasks[] array according to

        * the structure required by SimpleExpandableListAdapter.

        */

            <strong>private</strong> <span style="text-decoration: underline;">List</span> createGroupList() {

              <span style="text-decoration: underline;">ArrayList</span> result = <strong>new</strong> <span style="text-decoration: underline;">ArrayList</span>();

              <strong>for</strong>( <strong>int</strong> i = 0 ; i < <em>tasks</em>.length ; ++i ) {

                  <span style="text-decoration: underline;">HashMap</span> m = <strong>new</strong> <span style="text-decoration: underline;">HashMap</span>();

                <span style="text-decoration: underline;">m.put( </span><span style="text-decoration: underline;">"taskName"</span><span style="text-decoration: underline;">,</span><em><span style="text-decoration: underline;">tasks</span></em><span style="text-decoration: underline;">[i] )</span>;

                  <span style="text-decoration: underline;">result.add( m )</span>;

              }

              <strong>return</strong> (<span style="text-decoration: underline;">List</span>)result;

          }

      /**

       * Creates the child list of the taskAdds[][] array according

       * to the structure required by SimpleExpandableListAdapter

       */

        <strong>private</strong> <span style="text-decoration: underline;">List</span> createChildList() {

            <span style="text-decoration: underline;">ArrayList</span> result = <strong>new</strong> <span style="text-decoration: underline;">ArrayList</span>();

            <strong>for</strong>( <strong>int</strong> i = 0 ; i < <em>taskAdds</em>.length ; ++i ) {

      // Second-level lists

              <span style="text-decoration: underline;">ArrayList</span> secList = <strong>new</strong> <span style="text-decoration: underline;">ArrayList</span>();

              <strong>for</strong>( <strong>int</strong> n = 0 ; n < <em>taskAdds</em>[i].length ; n++ ) {

                <span style="text-decoration: underline;">HashMap</span> child = <strong>new</strong> <span style="text-decoration: underline;">HashMap</span>();

                  <span style="text-decoration: underline;">child.put( </span><span style="text-decoration: underline;">"taskAddName"</span><span style="text-decoration: underline;">, </span><em><span style="text-decoration: underline;">taskAdds</span></em><span style="text-decoration: underline;">[i][n] )</span>;

                  <span style="text-decoration: underline;">secList.add( child )</span>;

              }

              <span style="text-decoration: underline;">result.add( secList )</span>;

            }

            <strong>return</strong> result;

        }

      // Create menu and Context Menu

      @Override

      <strong>public</strong> <strong>void</strong> onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){

            <strong>super</strong>.onCreateContextMenu(menu, v, menuInfo);

            MenuInflater mi = getMenuInflater();

            mi.inflate(R.menu.<em>list_item_menu_longpress</em>, menu);

      }

      @Override

      <strong>public</strong> <strong>boolean</strong> onCreateOptionsMenu(Menu menu){

            <strong>super</strong>.onCreateOptionsMenu(menu);

            MenuInflater mi = getMenuInflater();

            mi.inflate(R.menu.<em>list_menu</em>, menu);

            <strong>return</strong> <strong>true</strong>;

      }

      //Handling Menu item selection

      <strong>public</strong> <strong>boolean</strong> onMenuItemSelected(<strong>int</strong> featureId, MenuItem item){

            <strong>switch</strong>(item.getItemId()){

            <strong>case</strong> R.id.<em>menu_insert</em>:

                  createReminder();

                  <strong>return</strong> <strong>true</strong>;

            }

            <strong>return</strong> <strong>super</strong>.onMenuItemSelected(featureId, item);

      }

      @Override

      <strong>public</strong> <strong>boolean</strong> onContextItemSelected(MenuItem item){

            <strong>switch</strong>(item.getItemId()){

            <strong>case</strong> R.id.<em>menu_remove</em>:

                  //delete the task

                  <strong>return</strong> <strong>true</strong>;

            }

            <strong>return</strong> <strong>super</strong>.onContextItemSelected(item);

      }   

      <strong>private</strong> <strong>void</strong> createReminder() {

            Intent i = <strong>new</strong> Intent(ReminderListActivity.<strong>this</strong>, ReminderActivityEdit.<strong>class</strong>);

            startActivityForResult(i, <em>ACTIVITY_CREATE</em>);

      }

      @Override

      <strong>protected</strong> <strong>void</strong> onActivityResult(<strong>int</strong> requestCode, <strong>int</strong> resultCode, Intent intent){

            <strong>super</strong>.onActivityResult(requestCode, resultCode, intent);

            //Reload the list here

      }

}

As you can see from when the adapter is called the first group is the parent group then is followed by the childgroup. This is of course a basic utilization of the expandableListView and uses a program based list to populate the listview. What you would want to do is to have the list populated from a database. I will be adding that tutorial later. If there is anything that you would like to see done please ask and I will do my best to show it.

About these ads

About msprayberry

Avid Android application developer.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s