我有一个ListView
(在Activity
中,而不是在ListActivity
中),它使用自定义光标适配器从本地数据库中获取一些数据,并将其显示在ListView
中。
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id)
{
Log.i(tag, "position = " + position);
Log.i(tag, "id is : " + id));
}
});
假设我的数据库列如下:
ID,姓名,姓氏,出生日期,身高,性别,placeOfBirth和maritalStatus。
但是,我在列表视图(row.xml)中仅显示名称和姓氏。
但是,每当用户单击列表中的某一行时,我也要检索其余数据,例如id或所单击行的性别。
问题是,我没有在列表中显示数据库行中的所有信息,但是,当用户按列表时,我需要检索该列表的一些数据。我该怎么办?
下面的方法不起作用,因为我不在ListActivity中,而在Activity中。
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
c.moveToPosition(position);
//get the data...
}
最佳答案
arg0
中的OnItemClickListener
参数是ListView
。但是,你可以做
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
// This will get the cursor from the adapter, already moved to position
Cursor cursor = (Cursor) mCursorAdapter.getItem(position)
//get the data...
}
});