在为行添加布局XML的按钮之前,在单击列表项时,在回调onListItemClick()中返回行ID.现在我在列表行布局中添加了一个按钮,此回调不再起作用.我读到这是正常的.我已经能够通过在我的列表行的布局XML文件中包含这种东西来获取文本和新按钮来引用新的回调:
<Button
android:onClick="newCallBackFunctionName"/>
Run Code Online (Sandbox Code Playgroud)
问题是我似乎无法找到一种方法来检索与按下的特定按钮所在的列表项对应的行ID号.在onListItemClick()的情况下,它作为回调的一部分传递,但在上面的例子中,只有被点击的View对象被传递回回调newCallBackFunctionName.我该怎么办?
*编辑:我的列表由SimpleCursorAdaptor填充,以防重要.
*编辑:我的列表和列表行XML布局如下:
列表:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/basic_linear_layout_v1">
<TextView
android:id="@+id/category_selection_page_name"
style="@style/page_heading_v1"/>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView
android:id="@id/android:empty"
style="@style/problem_text_v1"
android:text="@string/search_list_empty" />
<Button
android:id="@+id/select_category_button"
android:textSize="20sp"
android:layout_weight="10"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:onClick="reload"/>
Run Code Online (Sandbox Code Playgroud)
行:
<?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:orientation="horizontal">
<TextView android:id="@+id/category_browse_name" style="@style/basic_list_item_v1"
android:layout_weight="1"
android:layout_gravity="left"
android:focusable="true"
android:clickable="true"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right">
<Button
android:text="..."
android:id="@+id/subcategories_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusable="true"
android:onClick="onSubcategoryButtonClick">
</Button>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
您可以在适配器的getView方法中使用适当的id或位置标记您的按钮.例如:
myButton.setTag(id);
Run Code Online (Sandbox Code Playgroud)
然后在onClick处理程序中,从单击的视图中检索标记.例如:
public void newCallBackFunctionName(View v) {
long id = (Long) v.getTag();
// ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6754 次 |
| 最近记录: |