androidtabsexpandablelistviewexpandablelistadapter

Add ExpandableListView to a tab with other elements


I am developing an app with tabs. On one of the tabs, I have two EditText fields and a couple of buttons. I want to add an ExpandableListView to the tab, below the rest of the elements.

My problem is that the current class extends Activity. All the example I've found extend ExpandableListActivity, but I can't extend both for the same class.

How can I add this list to the tab? I've added an ExpandableListView element in the XML file, but whenever I run it, it doesn't show up. I understand that I need to bind the view to some data, but how do I go about doing this? Thank you.

Here is my class code:

public class DirectionsTab extends Activity 
{
    private Button clear_btn;
    private Button go_btn;
    private EditText origin_txt;
    private EditText dest_txt;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.directions_tab);

        // initialize views and onClick event handler
        origin_txt = (EditText)findViewById(R.id.origin_txt);
        dest_txt = (EditText)findViewById(R.id.dest_txt);
        clear_btn = (Button)findViewById(R.id.clear_btn);
        // clear all text fields and return focus to dest_txt
        clear_btn.setOnClickListener(new OnClickListener(){ 
            @Override
            public void onClick(View v)
            {
             origin_txt.setText("");
             dest_txt.setText(""); 
             origin_txt.requestFocus(); 
            }    
        });        
    } // end public void onCreate(Bundle savedInstanceState)
} // end public class DirectionsTab extends Activity 

and XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/origin_lbl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Origin"/>
    <EditText
        android:id="@+id/origin_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/origin_lbl"/>

    <TextView
        android:id="@+id/dest_lbl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/origin_txt"
        android:text="Destination" />
    <EditText
        android:id="@+id/dest_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/dest_lbl" />    

    <ExpandableListView
     android:id="@+id/listView"      
     android:layout_below="@+id/dest_txt" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" /><!--android:id="@android:id/list"-->

    <Button style="?android:attr/buttonStyleSmall"
        android:id="@+id/clear_btn"        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/dest_txt"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"        
        android:layout_marginTop="10dip"
        android:text="Clear" />
    <Button style="?android:attr/buttonStyleSmall"
        android:id="@+id/go_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:layout_toLeftOf="@id/clear_btn"
        android:layout_alignTop="@id/clear_btn"
        android:text="Go!" />
</RelativeLayout>

Solution

  • I got this!, If anyone is having problems with this, make sure you follow these steps:

    1. Copy and paste ExpandableList3 example (located in ApiDemos project from Samples folder in your Android SDK). This is just a good example of how to set up a list.
    2. Make your class inherit from ExpandableListActivity
    3. Create the following in your XML layout file:

      <ExpandableListView
      android:id="@android:id/list"       
      android:layout_below="@+id/go_btn" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"
      android:layout_weight="1"/>
      

    It is VERY important that the ID property matches what you see above, otherwise, nothing will work. This little detail wasted plenty of hours for me and was discovered seating neatly as a small detail in the android docs.