首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListView on OnItemLongClicklistener -检索信息

ListView on OnItemLongClicklistener -检索信息
EN

Stack Overflow用户
提问于 2014-09-17 00:24:30
回答 1查看 47关注 0票数 0

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

到目前为止,我可以长时间单击行项并检索行ID (此部分工作),但是我不相信这是我想要的最终结果。我希望能够检索与行项关联的数据库中的主键;此信息将显示在行项中。我似乎不能走那么远;我不知道如何将list视图中的行id“连接”回行中的信息(例如主键和其他文本)。下面是我到目前为止的代码。

代码语言:javascript
复制
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。

代码语言:javascript
复制
<?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>

我已经搜索过这个,但我似乎找不到一个具体解决这个问题的答案

这个答案能够让我走上正确的轨道,直到现在为止,我的地址是长时间点击:can't getting data from cursor adapter

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-17 01:21:43

我让它起作用了。以下是我所改变的:

代码语言:javascript
复制
 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);

代码语言:javascript
复制
OnItemLongClickListener listener = new OnItemLongClickListener(){
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long id){

                TextView test = (TextView) view.findViewById(R.id.descriptionList);

                description_string = test.getText().toString();
                Toast.makeText(getApplicationContext(), "Long Clicked " + description_string, Toast.LENGTH_SHORT).show();
                return false;
            }
        };
        lv.setOnItemLongClickListener(listener);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25880436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档