从 sql 填充微调器并使结果唯一
再次向我问好(抱歉)
我很累从 sqlitle 填充微调器,它工作正常,但我的表看起来像:
_id |名称 |时间 | ....
_id:是自增量号,始终唯一 名称:是人名,在该表中的许多行上重复
我想将这一行中的所有名称放入微调器中,但 uniq 没有重复项。 我现在是怎么做的:
private void updateSpiner(){ Spinner nameSpinner = (Spinner) findViewById(R.id.spinMena);
Cursor nameCursor = db.getNames();
startManagingCursor(nameCursor);
String[] from = new String[]{"Name"};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter nameAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, nameCursor, from, to);
nameSpinner.setAdapter(nameAdapter);
}
这是我获取数据的方式:
public Cursor getNames(){
final String KEY_TITLE = "Name";
final String KEY_ROWID = "_id";
return database.query("plan", new String[] {KEY_ROWID, KEY_TITLE}, null, null, null, null, null);
}
有人可以向我解释为什么我还需要为此获取 _id 行,我现在仍然使用 Java,我不明白为什么我需要它,除非我不这样做它会抱怨没有_id。
但它没有在第一种方法中使用,我只从中得到名称...因为 _id 我不能在查询 DISTINCT = true 中使用,因为它仍然返回所有行,因为 _id 对于每行都是唯一的。
有人可以帮助我吗把我的头围起来吗?我不想制作单独的桌子或其他东西..
谢谢, 弗拉德
Hello me again ( sorry )
I'm tiring to populate spinner from sqlitle and it is working fine BUT my table looks like:
_id | Name | Time | ....
_id: is self incremental number always unique
Name: is name of person, repeats on many lines in this table
I want to put in spinner all names from this row but uniq no duplicates.
how I'm doing it now:
private void updateSpiner(){
Spinner nameSpinner = (Spinner) findViewById(R.id.spinMena);
Cursor nameCursor = db.getNames();
startManagingCursor(nameCursor);
String[] from = new String[]{"Name"};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter nameAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, nameCursor, from, to);
nameSpinner.setAdapter(nameAdapter);
}
here is how I get the data:
public Cursor getNames(){
final String KEY_TITLE = "Name";
final String KEY_ROWID = "_id";
return database.query("plan", new String[] {KEY_ROWID, KEY_TITLE}, null, null, null, null, null);
}
can somebody explain to me why I need to get also _id row for this, I'm still now to Java and I don't see why I need it, except if I don't do it it will complain that there is no _id.
Buts it is not used in first method I get only Names from it... because of _id I can't use in query DISTINCT = true because it still return all rows as _id is unique for each line..
Could someone help me to wrap my head around it ? I don't want to make separate table or something..
Thanks,
Vlad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就我而言,我无法使用 SimpleCursorAdapter 完成这项工作,我必须做的是,使用 ArrayAdapter 而不是 simplecursoradapter 并在从查询中删除 ID 字段后在查询中添加 DISTINCT 子句。这是讨论为什么 simplecursor 适配器需要 _id 字段的链接
In my case, I couldn't get this work with SimpleCursorAdapter, What I had to do was, use ArrayAdapter instead of simplecursoradapter and add DISTINCT clause in the query after removing ID field from the query. Here is link for discussion on Why _id field required for simplecursor adapter
在我看来,您的表格不太适合 Spinner 输入。您需要规范化数据库并创建一个类似于 nameId | 的新表。名称。然后在“数据”表中您将拥有
dataId |名称 ID |时间 | ...
。然后,您只需使用新nameId | 中的数据加载 Spinner 即可。名称
表。It seems to me that your table is not ideally suited for a Spinner input. You need to normalize your database and create a new table that looks something like
nameId | name
. Then in the "data" table you would havedataId | nameId | time | ...
. Then you just load up your Spinner with the data from the newnameId | name
table.也许你可以在sqlite查询中使用DISTINCT:SELECT手册
Maybe You can use DISTINCT in sqlite query: SELECT manual