简体   繁体   中英

ListView on OnItemLongClicklistener - retrieving information

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

well I have it working. Here is what I changed:

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM