如何从一个ListView,它包含一个可点击的项目获得该行的行ID? [英] How do I get the row ID of the row from a ListView, which contains a clickable item?

查看:111
本文介绍了如何从一个ListView,它包含一个可点击的项目获得该行的行ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我添加了一个按钮的布局XML为我行,行ID是在回调onListItemClick()在列表项返回被点击。现在,我添加了一个按钮,列表行布局,这种回调不工作了。我看,这是正常的。我已经能够获取文本,新的按钮,通过我的列表行布局XML文件中包含这样的事情来引用新的回调:

Before I added a button to the layout XML for my rows, the row ID was returned in the callback onListItemClick() upon a list item being clicked. Now that I added a button to the list row layout, this callback doesn't work anymore. I read that this is normal. I have been able to get the text and the new button to refer to new callbacks via the inclusion of this sort of thing in the layout XML file for my list row:

<Button
    android:onClick="newCallBackFunctionName"/>

现在的问题是,我似乎无法找到一个方法来检索对应的列表项的行ID号中,这是pressed所在的特定按钮。在onListItemClick的情况下()这是通过为回调的一部分,但在上述情况下,只有被点击View对象被传递回回调newCallBackFunctionName。我能做些什么呢?

The problem is that I can't seem to find a way to retrieve the row id number corresponding to the list item in which the particular button that was pressed lies. In the case of onListItemClick() this was passed as part of the callback, but in the above case only the View object that is clicked is passed back to the callback newCallBackFunctionName. What can I do about this?

*编辑:我的目录由SimpleCursorAdaptor填充,如果这是很重要

* My list is populated by a SimpleCursorAdaptor, in case that's important.

*编辑:我的目录和列表行XML布局如下:

* My list and list row XML layouts are as follows:

清单:

<?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"/>

行:

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

推荐答案

您可以在您的适配器 getView 方法,适当的ID或位置来标记你的按钮。例如:

You can tag your Buttons with the appropriate id or position in your adapter's getView method. For example:

myButton.setTag(id);

然后在你的onclick处理程序,检索被点击视图的标签。例如:

Then in your onClick handler, retrieve the tag from the view that was clicked. For example:

public void newCallBackFunctionName(View v) {
    long id = (Long) v.getTag();
    // ...
}

这篇关于如何从一个ListView,它包含一个可点击的项目获得该行的行ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆