简体   繁体   English

从Android SQlite数据库获取列表视图的行ID

[英]Getting the row ID of a listview from a Android SQlite database

I have created a listview which displays all the people in the database. 我创建了一个列表视图,该视图显示数据库中的所有人员。 But how do I get the row id of the one I select so I can pass that infomation to the next activity. 但是,如何获取所选的行ID,以便将信息传递给下一个活动。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.load_game1);

    listContent = (ListView)findViewById(R.id.contentlist);

    mDbAdapter = new DbAdapter(this);
    mDbAdapter.open();
    Cursor cursor = mDbAdapter.fetchAll();
    startManagingCursor(cursor);
    String[] from = new String[]{DbAdapter.KEY_NAME, DbAdapter.KEY_ROWID};
    int[] to = new int[]{R.id.text};
    cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
    listContent.setAdapter(cursorAdapter);
    mDbAdapter.close();
    }

Which displays displays http://i.minus.com/iGEtGKGyF1Wgc.png 哪个显示屏显示http://i.minus.com/iGEtGKGyF1Wgc.png

Now I want to click on a name get that row id from the database. 现在,我想单击一个名称,从数据库中获取该行ID。

使用此API调用: cursorAdapter.getItemId(positionInListView)

Use setOnItemClickListener on your Listview then you will get the id of the selected item (item id start from 0 but Database _id column is start from 1 so you should send id+1 , please confirm this). Listview上使用setOnItemClickListener ,然后您将获得所选项目的ID(项目ID从0开始,但Database _id列从1开始,因此您应该发送id+1 ,请确认)。

And send the id using intents. 并使用意图发送ID。

You have to register implement and add an OnItemClickListener to the list so you can react when an item was clicked. 您必须注册工具并将一个OnItemClickListener添加到列表中,以便您可以在单击某个项目时做出反应。 The item id will be passed into the onListItemClick() method of the OnItemClickListener. 商品ID将传递到OnItemClickListener的onListItemClick()方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM