如何将项目添加到Si​​mpleCursorAdapter? [英] How to add an item to SimpleCursorAdapter?

查看:131
本文介绍了如何将项目添加到Si​​mpleCursorAdapter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两列_id和称号一个简单的数据库表。
而我显示的微调器中的数据,而且效果很好。

I have a simple database table with 2 columns "_id" and "title". and I'm displaying the data in a spinner, and it works well.

但我需要在微调列表的顶部,是不是与ID = 0和title =未指定数据库中增加一个项目;

but I need to add one more item at the top of the spinner list that is not from the database with id = 0 and title = "not specified";

Spinner list = (Spinner) findViewById(R.id.spinner);
Cursor cursor = database.getAll(); // returns cursor with objects

String[] columns = new String[] {"title"};
int[] to = new int[] {R.id.title};

list.setAdapter(new SimpleCursorAdapter(this, R.layout.object_item_simple, cursor, columns, to));

我需要知道从数据库中选择的项目编号,我可以用 list.getSelectedItemId()做到这一点;

所以我不能改用ArrayAdapter SimpleCursorAdapter的,因为我不认为有一个设置id为适配器上每个项目的方法。
有没有办法做到这一点?

so I can't use ArrayAdapter instead of SimpleCursorAdapter, because i don't think that there is a method for setting the id for each item on the adapter. is there a way to do this?

感谢。

推荐答案

您可以从您的ID和标题创建一个对象,并建立这些对象与光标的列表。然后将在该列表的顶部顶部的人工输入。

You could create an object out of your id and title and build a list of these objects with the cursor. Then insert your artificial entry at the top top of that list.

然后,当您构建在这个列表中的适配器通。

Then when you construct your Adapter pass in this list.

另外,你可以把一个虚拟值到你的数据库,尽管这将是不可思议,也许不可能根据您的查询和数据。一个ArrayAdapter是更明智的。

Alternatively you could put a dummy value into your database, although that would be weird and maybe not possible depending on your query and data. The ArrayAdapter is much more sensible

这篇关于如何将项目添加到Si​​mpleCursorAdapter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆