我目前正在用清单视图对一个APP进行编程,该清单视图充满了来自sqlite表的数据,如下所示:
public voID printDatabase(){ Cursor c = handler.getAllRows(); String[] fIEldnames = new String[] {DBHandler.ColUMN_WORKER_ID, DBHandler.ColUMN_WORKER_name, DBHandler.ColUMN_WORKER_SURname, DBHandler.ColUMN_WORKER_COST}; int[] toVIEw = new int[] {R.ID.item_worker_ID, R.ID.item_worker_name, R.ID.item_worker_surname, R.ID.item_worker_cost}; SimpleCursorAdapter cAdapter; cAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.worker_items, c, fIEldnames, toVIEw, 0); List = (ListVIEw) findVIEwByID(R.ID.worker_ListVIEw); List.setAdapter(cAdapter);}
现在,我想通过单击项目从列表视图中获取数据.我到处搜索以找到解决方案,结果如下:
class ItemListener implements AdapterVIEw.OnItemClickListener{ @OverrIDe public voID onItemClick(AdapterVIEw<?> adapterVIEw, VIEw vIEw, int i, long l) { Object o = List.getItemAtposition(i); String s = List.toString(); }}
但是我从中得到的只是对我用于填充列表视图的游标的引用.我需要什么来从列表视图中获取数据?
解决方法:
您已经解决了一半.
Cursor itemCursor = (Cursor) List.getItemAtposition(i);
这将为您返回一个光标,该光标指向被单击的行.您可以像这样获取数据:
class ItemListener implements AdapterVIEw.OnItemClickListener { @OverrIDe public voID onItemClick(AdapterVIEw<?> adapterVIEw, VIEw vIEw, int i, long l) { Cursor itemCursor = (Cursor) List.getItemAtposition(i); String workerID = itemCursor.getString(itemCursor.getColumnIndex(DBHandler. ColUMN_WORKER_ID)); String workername = itemCursor.getString(itemCursor.getColumnIndex(DBHandler.ColUMN_WORKER_name)); String workerSurname = itemCursor.getString(itemCursor.getColumnIndex(DBHandler.ColUMN_WORKER_SURname)); String workerCost = itemCursor.getString(itemCursor.getColumnIndex(DBHandler.ColUMN_WORKER_COST)); }}
总结 以上是内存溢出为你收集整理的Android从列表视图中获取数据全部内容,希望文章能够帮你解决Android从列表视图中获取数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)