以下是我正在使用的内容:我有一个ExpandableListView,它使用SimpleCursorTreeAdapter来填充SQLite数据库中的视图。我正在使用自定义组和子视图,组和子视图都有按钮。我需要知道如何在这些按钮上设置按钮监听器,以便在调用监听器时,我知道它来自哪个行(或光标id)。我能够在onChildItemDetailsClicked()上设置一个工作监听器,但在监听器内部我不知道哪一行对应按钮按下。有什么想法吗?
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();
}
}
组视图:
<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>
儿童观点:
<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>