开发者

How to get value from database with onListItemClick?

I can add to a db and list as a listview. When I click a list item using onListItemClick, what statement do I need to get the value?

String[] from = new String[] {NotesDbAdapter.KEY_BODY, NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_NUMBER};
int[] to = new int[] {R.id.toptext, R.id.middletext, R.id.circle};  
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.row, notesCursor, from, to);
setListAdapter(notes);

//some other code
protected void onListItemClick(ListView l, View v, int position, long thisID)
{
    super.onListItemClick(l, v, position开发者_运维百科, thisID);
    **String **resposponseid** = Activity2.getData();**
}

I have responseid for every row unique which I want to use when I click on list position. So how to get responseid when I click on list?


In general, it should something like this. You will need to tailor it to suit your needs.

@Override
protected void onListItemClick(ListView l, View v, int position, long thisID)
{
    super.onListItemClick(l, v, position, thisID);
    // Get the item that was clicked
    Object o = this.getListAdapter().getItem(position);
    String keyword = o.toString();
}

I would suggest you go through a tutorial to learn, especially if you are new to Android and Java. Here's one - Android ListView and ListActivity - Tutorial.


In the onListItemClick method, the parameter int position is exactly what you're looking for. The list is filled by an ArrayAdapter which utilizes some List (let's say an ArrayList) to get Views for each row.

The fact that the View in each row can be pretty complicated (not consist of a single value), leads to the way it is implemented, and you don't get the value itself, but the position in the list. What you need to do to get the value is to query that ArrayList (its get(int index) method) you used to fill the ListView with the position param.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜