【发布时间】:2011-05-16 18:40:56
【问题描述】:
我讨厌由简单的光标适配器创建的列表:
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) {
我失败的部分是检索特定条目(行)的 BookTitle 部分,以便重新查询数据库并使用 AlertDialog.Builder 呈现数据。
当我尝试做的时候:
String selection = l.getItemAtPosition(position).toString();
我只得到了 android.database.sqlite SQLiteCursor@44f99e80 并且我很困惑应该如何做到这一点(我知道它为什么会崩溃;我不知道应该如何正确地做到这一点。
完整代码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();
} }
【问题讨论】: