Search code examples
javaandroidexpandablelistviewonclicklistenerexpandablelistadapter

Androids ExpandableListView - where to put the button listener for buttons that are children


I have been playing around a lot with the ExpandableListView and I cannot figure out where to add the button listeners for the button that will be the children in the view. I did manage to get a button listener working that uses getChildView() below, but it seems to be the same listener for all the buttons.

The best case scenario is that I would be able to implement the button listeners in the class that instantiates the ExpandableListAdapter class, and not have to put the listeners in the actual ExpandableListAdapter class. At this point I don't even know if that is possible

I have been experimenting with this tutorial/code: HERE

getChildView()

@Override
public View getChildView(int set_new, int child_position, boolean view, View view1, ViewGroup view_group1)
{
    ChildHolder childHolder;
    if (view1 == null)
    {

        view1 = LayoutInflater.from(info_context).inflate(R.layout.list_group_item_lv, null);

        childHolder = new ChildHolder();

        childHolder.section_btn = (Button)view1.findViewById(R.id.item_title);
        view1.setTag(childHolder);
        childHolder.section_btn.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                Toast.makeText(info_context, "button pushed", Toast.LENGTH_SHORT).show();

            }
        });



    }else {
        childHolder = (ChildHolder) view1.getTag();
    }
        childHolder.section_btn.setText(children_collection.get(set_new).GroupItemCollection.get(child_position).section);
        Typeface tf = Typeface.createFromAsset(info_context.getAssets(), "fonts/AGENCYR.TTF");

        childHolder.section_btn.setTypeface(tf);
        return view1;
}

Any help would be much appreciated. Thank you and I will be standing by.


Solution

  • If the buttons are in the ExpandableListView, their listener needs to be in the adapter.

    I'm not sure the thrust of your question, but if you are asking how do you relate the button to the contents of the child row, I can answer that. :p

    I'll assume a somewhat simple child row layout for demonstration purposes.

    child_row.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/ListItem1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left|center_vertical"
        android:paddingLeft="7dip"
        android:paddingRight="7dip"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/ListItem2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right|center_vertical"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <Button
        android:id="@+id/button"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>
    

    Then, to get the contents of the row when your button is pressed, you use the button to backtrack to the parent vieew and then get the necessary child views and their contents:

        childHolder.section_btn.setOnClickListener(new OnClickListener()    
        {    
            @Override    
            public void onClick(View v)    
            {    
                LinearLayout ll = (LinearLayout) v.getParent();    // get the view containing the button
                TextView tv1 = (TextView) ll.findViewById(R.id.ListItem1);  // get the reference to the first widget
                TextView tv2 = (TextView) ll.findViewById(R.id.ListItem2);  // get the reference to the second widget
                String text1 = tv1.getText.toString();  // Get the contents of the first widget to a string
                String text2 = tv2.getText.toString();  // Get the contents of the second widget to a string
            }    
        });  
    

    If this isn't what you were looking for clarify your question and I'll take another shot at it.