首页 \ 问答 \ OnItemLongClicklistner上的ListView - 检索信息(ListView on OnItemLongClicklistener - retrieving information)

OnItemLongClicklistner上的ListView - 检索信息(ListView on OnItemLongClicklistener - retrieving information)

我有一个列表视图,显示数据库中的信息。 我希望能够允许用户长按行项目。

到目前为止,我可以长按行项目并检索行ID(此部分有效),但我不相信这是我想要的最终结果。 我希望能够检索与行项关联的数据库中的主键; 此信息显示在行项目中。 我似乎无法走得那么远; 我无法弄清楚如何将列表视图中的行ID“连接”回行中的信息(例如主键和其他文本)。 下面是我到目前为止的代码。

DatabaseHandler db = new DatabaseHandler(this);
    SimpleCursorAdapter dataAdapter;

    @Override
    protected void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_main);




        Cursor cursor=db.getAllLocationsCursor();

        String from [] = new String[] {db.COLUMN_DESCRIPTION, db.COLUMN_LOCATION, db.KEY_ID};
        final int to [] = new int[]{R.id.descriptionList, R.id.locationList, R.id.idList};

        dataAdapter = new SimpleCursorAdapter(this, R.layout.row_item, cursor, from, to, 0);


        ListView lv = getListView();
        lv.setAdapter(dataAdapter);


        OnItemLongClickListener listener = new OnItemLongClickListener(){
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id){

                Toast.makeText(getApplicationContext(), "Long Clicked " + id, Toast.LENGTH_SHORT).show();
                return false;
            }
        };
        lv.setOnItemLongClickListener(listener);
    }

这是我的行项目xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:clickable="true"
    android:focusable="true"
    android:longClickable="true"
    android:onClick="itemClick" >

    <TextView
        android:id="@+id/descriptionList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/mainText" />

    <TextView
        android:id="@+id/locationList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/descriptionList"
        android:lines="2"
        android:text="TextView"
        android:textColor="@color/locationWordsText" />

    <TextView
        android:id="@+id/idList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:alpha="0"
        android:text="TextView" />

</RelativeLayout>

我已经搜索了这个,但我似乎无法找到专门解决这个问题的答案

这个答案能够让我在正确的轨道上找到解决长按的问题: 无法从光标适配器获取数据


I have a list view that is displaying information from a database. I want to be able to allow a user to long click on the row item.

so far, I can long click on the row item and retrieve the row ID (this part works), however I don't believe this the end result I want. I want to be able to retrieve the primary key in the database that is associated with the row item; this information is being displayed in the row item. I can't seem to get that far; I can't figure out how 'connect' the row id from the list view back to the information in the row (such as the primary key, and other text). Below is the code I have so far.

DatabaseHandler db = new DatabaseHandler(this);
    SimpleCursorAdapter dataAdapter;

    @Override
    protected void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_main);




        Cursor cursor=db.getAllLocationsCursor();

        String from [] = new String[] {db.COLUMN_DESCRIPTION, db.COLUMN_LOCATION, db.KEY_ID};
        final int to [] = new int[]{R.id.descriptionList, R.id.locationList, R.id.idList};

        dataAdapter = new SimpleCursorAdapter(this, R.layout.row_item, cursor, from, to, 0);


        ListView lv = getListView();
        lv.setAdapter(dataAdapter);


        OnItemLongClickListener listener = new OnItemLongClickListener(){
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id){

                Toast.makeText(getApplicationContext(), "Long Clicked " + id, Toast.LENGTH_SHORT).show();
                return false;
            }
        };
        lv.setOnItemLongClickListener(listener);
    }

This is my row item xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:clickable="true"
    android:focusable="true"
    android:longClickable="true"
    android:onClick="itemClick" >

    <TextView
        android:id="@+id/descriptionList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/mainText" />

    <TextView
        android:id="@+id/locationList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/descriptionList"
        android:lines="2"
        android:text="TextView"
        android:textColor="@color/locationWordsText" />

    <TextView
        android:id="@+id/idList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:alpha="0"
        android:text="TextView" />

</RelativeLayout>

I've searched for this, but I can't seem to find an answer that specifically addresses this

This answer was able to get me on the right track as far a addressing the long click: can't getting data from cursor adapter


原文:https://stackoverflow.com/questions/25880436
更新时间:2024-02-03 16:02

最满意答案

这实际上是不可能的,使用公共API(除了使用重新定义方法)。 你可能有一些运气使用私人反思,但当然是一种脆弱的方法。 我们将研究在下一个版本的剃刀中使这种情况更容易。

在此期间,我写了几篇关于这个主题的博文:

  • http://blogs.msdn.com/b/marcinon/archive/2010/12/08/optional-razor-sections-with-default-content.aspx
  • http://blogs.msdn.com/b/marcinon/archive/2010/12/15/razor-nested-layouts-and-redefined-sections.aspx

This is in fact not possible today using the public API (other than using the section redefinition approach). You might have some luck using private reflection but that of course is a fragile approach. We will look into making this scenario easier in the next version of Razor.

In the meantime, here's a couple of blog posts I've written on the subject:

相关问答

更多
  • 你知道吗,没关系。 在我删除的部分(数字!)我调用Azure分布式缓存。 结果新的缓存客户端需要200毫秒!...我很惊讶。 无论如何,修复解决了它 You know what, nevermind. In the part I cut out (figures!) I have a call to the Azure distributed cache. Turns out new-ing up a cache client takes 200 ms!...Im surprised at that. An ...
  • 我知道这是一个古老的问题。 无论如何,如果有其他人遇到这种情况,我想我会分享这一点(就像我一样)。 在您的子布局的底部,您将定义一个与父布局中的部分具有相同名称的部分。 在本节内部,您只需放置一个@RenderSection ,再次指定与以前相同的名称。 一旦到位,您就基本上拥有了从页面到其父级布局的子布局“绕过”内容: @section breadcrumbs { @RenderSection("breadcrumbs", true) } I know it's an old question. ...
  • 这实际上是不可能的,使用公共API(除了使用重新定义方法)。 你可能有一些运气使用私人反思,但当然是一种脆弱的方法。 我们将研究在下一个版本的剃刀中使这种情况更容易。 在此期间,我写了几篇关于这个主题的博文: http://blogs.msdn.com/b/marcinon/archive/2010/12/08/optional-razor-sections-with-default-content.aspx http://blogs.msdn.com/b/marcinon/archive/2010/12/ ...
  • 根据代码可读性的复杂性, GridBagLayout将作为最佳解决方案。 由于这个结果最适合GridBagLayout ,只要知道如何解决相同的约束就可以了:-) import java.awt.*; import javax.swing.*; public class GridBagLayoutExample { private GridBagConstraints gbc; private JButton[] buttons; public GridBagLayoutExa ...
  • 您将self作为所有布局的父级。 这有效地将布局设置为窗口的主要布局(相当于setLayout )。 你又这样做了...... 基本上,在删除旧布局之前,您无法设置其他布局。 您应该在控制台中看到几个警告。 会发生什么,第一个布局( self.hBox )被设置为主布局,其余部分被忽略。 但是,您的QGroupBox将窗口作为父窗口。 所以它全局插入(左上)。 但它没有布局,因此无法调整其尺寸,而且缩小了。 如果从布局构造函数中删除self ,您将获得所需的结果。 顺便说一句,在使用布局时,您可以省略布局内 ...
  • 样式可以具有父级,结果视图应用属性“自上而下”,即子样式将覆盖冲突的父样式属性。 代码我最终使用(我有一个包含多个分隔符的列表,我只想在最后一个填充):