简体   繁体   中英

Get value of ListView item in Android

I am filling my ListView with data which I am getting from the database I created. I want to get the name of the item in ListView which is being long clicked. I have tried using following method:

 - parent.getItemAtPosition(position).toString();
 - myListView.getItemAtPosition(position).toString();
 - myListView.getSeletedItem(position).toString();

These three statements work well but as I am filling up the LisView by getting data from my database so I am getting the following value returned:

09-20 13:01:22.370: I/System.out(5351): android.database.sqlite.SQLiteCursor@426925b0

whereas the Item's name, which I am clicking, is 'Home'..

Please help me. How can I convert android.database.sqlite.SQLiteCursor@426925b0 into Home ?

MY ADAPTER:

mCursor = mDB.fetchData();
String[] columns = new String[] { AreaDatabase.KEY_AREA };
    int[] to = new int[] { R.id.tvArea };

    mAdapter = new SimpleCursorAdapter(this, R.layout.lvarea, mCursor,
            columns, to);
    lvArea.setAdapter(mAdapter);

fetchData() function:

public Cursor fetchAreaData() {
    Cursor mCursor = ourDatabase.query(AREA_TABLE_NAME, new String[] {
            KEY_ROWID, KEY_AREA }, null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }

    return mCursor;
}

Cursor has method getString(int columnIndex) . You can use that to get String value from Cursor from specified column index.

> Cursor currentCur = myListView.getItemAtPosition(position);
> String name = currentCur.getString(1); //It will return KEY_AREA (column index 1) value from Cursor

这样做:

parent.getItemAtPosition(position).toString();

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