SRa*_*Ram 0 android android-listview
我可以添加到数据库并列表作为列表视图.当我使用onListItemClick单击列表项时,我需要什么语句来获取值?
提前致谢.
public class Main extends ListActivity {
private static String[] FROM = { _ID, DESCRIPTION, UN };
private static String ORDER_BY = DESCRIPTION + " ASC";
private static int[] TO = {R.id.itemrowid, R.id.itemdescription, R.id.itemun };
private EventsData events;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
events = new EventsData(this);
try {
Cursor cursor = getEvents();
showEvents(cursor);
} finally {
events.close();
}
**public void onListItemClick(ListView parent, View v, int position, long id) {
// What statement to put here to get the value of _ID,DESCRIPTION, UN
// selected**?
}
private Cursor getEvents() {
// Perform a managed query. The Activity will handle closing
// and re-querying the cursor when needed.
SQLiteDatabase db = events.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null,
null, ORDER_BY);
startManagingCursor(cursor);
return cursor;
}
private void showEvents(Cursor cursor) {
// Set up data binding
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.item, cursor, FROM, TO);
setListAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)
首先,您需要在点击位置获取项目:
Cursor cursor = getListAdapter().getItem(position);
Run Code Online (Sandbox Code Playgroud)
然后你可以从这个光标获取数据(我不知道你使用什么类型,所以只是例如它将是int
和String
):
int id = cursor.getInt(cursor.getColumnIndex(_ID));
String description = cursor.getString(cursor.getColumnIndex(DESCRIPTION));
Run Code Online (Sandbox Code Playgroud)
请参阅Cursor可用方法的文档.
归档时间: |
|
查看次数: |
2122 次 |
最近记录: |