Here is what I'm working with: I have an ExpandableListView that uses a SimpleCursorTreeAdapter to fill the view from an SQLite Database. I'm using custom group and child views, and the group and child views both have buttons in them. I need to know how I can set a button listener on these buttons so that when the listener is called, I know what row (or cursor id) it came from. I am able to set a working listener onChildItemDetailsClicked(), but inside the listener I do not know which row corresponds to the button press. Any ideas?
public class MyListActivity extends ExpandableListActivity
{
private DatabaseHelper mDbHelper;
private Cursor mGroupCursor;
private int mGroupIdColumnIndex;
private ExpandableListAdapter mAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mDbHelper = new DatabaseHelper(this);
mDbHelper.openDatabase();
this.getExpandableListView().setItemsCanFocus(true);
fillData();
}
private void fillData()
{
mGroupCursor = mDbHelper.getAllGroupsCursor(); // fills cursor with list of top nodes - groups
startManagingCursor(mGroupCursor);
// Cache the ID column index
mGroupIdColumnIndex = mGroupCursor.getColumnIndex(DatabaseHelper.COL_ID);
// Set up our adapter
String[] groupCols = new String[] { DatabaseHelper.COL_TITLE, DatabaseHelper.COL_TEXT };
int[] groupViews = new int[] { R.id.group_title, R.id.group_text };
String[] childCols = new String[] {DatabaseHelper.COL_TEXT };
int[] childViews = new int[] {R.id.child_text };
mAdapter = new ExamineActivityAdapter(this, mGroupCursor,
R.layout.mylist_group_row, groupCols, groupViews,
R.layout.mylise_child_row, childCols, childViews);
setListAdapter(mAdapter);
}
@Override
public void onDestroy()
{
mDbHelper.close();
super.onDestroy();
}
public class MyListActivityAdapter extends SimpleCursorTreeAdapter
{
public MyListActivityAdapter(Context context, Cursor cursor,
int groupLayout, String[] groupFrom, int[] groupTo,
int childLayout, String[] childFrom, int[] childTo)
{
super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo);
}
// returns cursor with subitems for given group cursor
@Override
protected Cursor getChildrenCursor(Cursor groupCursor)
{
Cursor childCursor = mDbHelper.getChildrenCursorForGroup(groupCursor.getInt(mGroupIdColumnIndex));
startManagingCursor(childCursor);
return childCursor;
}
}
public void onChildItemDetailsClicked(View v)
{
Toast toast = Toast.makeText(this, "child detail clicked.", Toast.LENGTH_SHORT);
toast.show();
}
}
Group View:
<TextView
android:id="@+id/group_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="20sp"
android:singleLine="true"
/>
<Button
android:id="@+id/group_detail_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:textSize="12sp"
android:text="@string/button_detail"
android:focusable="false"
android:layout_alignParentRight="true"
android:layout_below="@id/group_title"
android:layout_marginLeft="10dip" />
<TextView
android:id="@+id/group_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textSize="12sp"
android:lines="3"
android:layout_toLeftOf="@id/group_detail_button"
android:layout_alignTop="@id/group_detail_button"
/>
</RelativeLayout>
Child View:
<TextView android:id="@+id/child_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip"
android:gravity="center_vertical" />
<Button android:id="@+id/child_detail_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:textSize="12sp"
android:text="@string/button_detail"
android:focusable="false"
android:onClick="onChildItemDetailsClicked"
android:layout_alignParentRight="true"
android:layout_below="@id/child_text"
android:layout_marginLeft="10dip" />
<CheckBox android:id="@+id/child_check"
android:focusable="false"
android:text="@string/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/child_text"
android:layout_toLeftOf="@id/child_detail_button"
android:layout_alignTop="@id/child_detail_button" />
</RelativeLayout>
Set ViewBinder
to your adapter and there, add listener for buttons.
this can help.