我想檢索Listview中某一個項目的ID。現在只能獲得位置。如何檢索Listview中某一個位置的ID?
public class OSListActivity extends ListActivity {
................
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
................
................
MatrixCursor cursor;
cursor = datasource.getnameList();
if (cursor.moveToFirst()) {
startManagingCursor(cursor);
String vdstatus = cursor.getString(6);
System.out.println("vdstatus : " + vdstatus);
String[] from = { "name", "desc", "status", "path", "folder",
BaseColumns._ID };
int[] to = { R.id.name, R.id.desc, R.id.status, R.id.path };
final VSsimplecursoradapter adapter = new VSsimplecursoradapter(
this, R.layout.row, cursor, from, to);
setListAdapter(adapter);
}
}
@Override
public void onListItemClick(ListView parent, View view, int position,
long id) {
Intent intent = new Intent(this, VSDetailsActivity.class);
intent.putExtra("com.manager.boot.r1223."+BaseColumns._ID, position);//TODO put _id instead of position
startActivity(intent);
}
}
Edit: 解決
public void onListItemClick(ListView parent, View view, int position,
long id) {
// Starts TestDetails activity on clicking a list item.
Cursor cursor = (Cursor) adapter.getItem(position);
Intent intent = new Intent(this, VSDetailsActivity.class);
intent.putExtra("com.manager.boot.r1223."+BaseColumns._ID, cursor.getInt(cursor
.getColumnIndex("_id")));
startActivity(intent);
}
你的ID是指哪個啊? onListItemClick(ListView parent, View view, int position, long id) 這裡不是有一個了嗎?如果是數據庫裡那個id,你有位置position,又有cursor了,可以通過cursor獲取啊。