Getting listItem data from a list
I hate a list created by simple cursor adapter:
Cursor c = myDbHelper.rawQ(select);
startManagingCursor(c);
// the desired columns to be bound
String[] columns = new String[] { "Books.BookTitle",
"Publishers.Publisher" };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.ISBN_entry, R.id.Title_entry };
// Getting results into our listview
try {
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
R.layout.listlayout, c, columns, to);
this.setListAdapter(mAdapter);
} catch (Exception e) {
}
The layout involved with the list are two simple textviews.
What i want to do is create a listener
@Override
protected void onListItemClick(ListView l, View v, int position, 开发者_StackOverflow社区long id) {
The part im failing at is retrieving the BookTitle part of the specific entry(row) in order to requery the database and present the data with AlertDialog.Builder.
When i try doing :
String selection = l.getItemAtPosition(position).toString();
i'm only getting android.database.sqlite SQLiteCursor@44f99e80 and im rather confused on how this should be done (I know why it's crashign just can;t get my mind around on how it should be done properly.
Full code atm:
...
Cursor c = myDbHelper.rawQ(select);
startManagingCursor(c);
// the desired columns to be bound
String[] columns = new String[] { "Books.BookTitle",
"Publishers.Publisher" };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.ISBN_entry, R.id.Title_entry };
// Getting results into our listview
try {
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
R.layout.listlayout, c, columns, to);
this.setListAdapter(mAdapter);
} catch (Exception e) {
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
//super.onListItemClick(l, v, position, id);
String selection = l.getItemAtPosition(position).toString();
new AlertDialog.Builder(v.getContext())
.setTitle("Title")
.setMessage(selection)
.setPositiveButton(android.R.string.ok, null)
.show();
} }
Try something like this...
Cursor theCursor = ((SimpleCursorAdapter)((ListView)l).getAdapter()).getCursor();
String selection = theCursor.getString(theCursor.getColumnIndex("Books.BookTitle"));
Just get the data from the cursor:
l.getItemAtPosition(position).getString(0); // it might be 1
See here.
精彩评论