如何从ListView位置检索_id

时间:2012-03-01 01:34:53

标签: android

我需要检索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);
    }
}

编辑:解决方案

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);
}

2 个答案:

答案 0 :(得分:0)

你已经知道了。查看方法的参数。查看“Long id”部分?这就是你要找的东西。

答案 1 :(得分:0)

我使用String s = ((Cursor) l.getItemAtPosition(position)).getString(0);并将其解析为长。