gpt4 book ai didi

android - 使用 SimpleCursorTreeAdapter 在 ExpandableListView subview 中单击按钮的监听器

转载 作者:行者123 更新时间:2023-11-29 00:40:31 26 4
gpt4 key购买 nike

这是我正在使用的:我有一个 ExpandableListView,它使用 SimpleCursorTreeAdapter 来填充来自 SQLite 数据库的 View 。我正在使用自定义组 View 和 subview ,并且组 View 和 subview 中都有按钮。我需要知道如何在这些按钮上设置按钮监听器,以便在调用监听器时,我知道它来自哪一行(或光标 ID)。我能够在 ChildItemDetailsClicked() 上设置一个工作监听器,但在监听器内部我不知道哪一行对应于按下的按钮。有什么想法吗?

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();
}
}

组 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>

subview :

    <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>

最佳答案

ViewBinder 设置为您的适配器,并在那里为按钮添加监听器。 this可以提供帮助。

关于android - 使用 SimpleCursorTreeAdapter 在 ExpandableListView subview 中单击按钮的监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9661493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com