从数据库中的Android SimpleCursorAdapter ID [英] Android SimpleCursorAdapter ID from Database
问题描述
我有几分的问题在我开发的应用程序。
所以,我所有的表都有主键_id领域,我用SimpleCursorAdapter将它们绑定到列表视图和纱厂。
我想知道的是,我怎么能在ListView或微调选择的项目有相同的ID作为相应的行?
I'm having somewhat of an issue in an application I'm developing. So, all my tables have _id fields for primary keys, and I use SimpleCursorAdapter to bind them to ListViews and Spinners. What I wanted to know is how can I make the ListView or Spinner selected item have the same ID as the corresponding row?
但奇怪的是,这个工程使用ContextMenu,我现在用直记事本的例子:
The strange thing is that this works with the ContextMenu, which I am using straight of the NotePad example:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
info.id
此ID字段的是一样rowid的桌子上,我可以删除项目很好,但是当我尝试这样的事:
This ID field IS the same as the RowID on the table, and I can delete items fine, but when I try something like this:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view,
int position, long arg) {
Intent i = new Intent(getApplicationContext(),
FuelingList.class);
long id = view.getId();
ID字段是一些随机rubberish。
所以我的问题是,在第一code位,标识是什么的 AdapterContextMenuInfo 的获取和我怎么能找回它在我的code的其他部分?
The ID field is some random rubberish. So my question is, in the first code bit, what Id is the AdapterContextMenuInfo getting and how can I retrieve it in other parts of my code?
推荐答案
由于您使用的是SimpleCursorAdapter,该onItemClick正在通过数据库行ID到你..
Since you are using a SimpleCursorAdapter, the onItemClick is passing the database row id in to you..
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg)
的长ARG
部分实际上是从数据库中的行ID。
The long arg
part is actually your row id from the database.
所以你的code应该是:
So your code should be:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view,
int position, long arg) {
Intent i = new Intent(getApplicationContext(),
FuelingList.class);
long id = arg;
这篇关于从数据库中的Android SimpleCursorAdapter ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!