SimpleCursorAdapter不会由于缺少_id的工作 [英] SimpleCursorAdapter won't work because of missing _id

查看:254
本文介绍了SimpleCursorAdapter不会由于缺少_id的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与一个光标和SimpleCursorAdapter一个问题。

i'm having a problem with a Cursor and a SimpleCursorAdapter.

我要完成什么:结果
我有3个字段的数据库结果
_id |昵称| somethingelse

What i want to accomplish:
I have a database with 3 fields
_id | nickName | somethingelse

我要清楚地选择所有的昵称,并在AutoCompleteTextView提供给他们。

I want to select all nickNames distinctly and provide them in an AutoCompleteTextView.

问题:结果
当进行查询(见下文),我没有选择_id字段。这就是为什么我试图创建SimpleCursorAdapter因为在光标没有场_id的时候得到一个错误。
但是,如果我在查询中选择_id,在光标的昵称将不再destinct!另外一个光标不modifyable据我所知是这样吗?

Problem:
When doing the query (see below) i don't select the _id-field. That's why i get an error when trying to create the SimpleCursorAdapter because in the cursor there is no field "_id". But if i select the "_id" in the query, the nickNames in the cursor won't be destinct anymore! Additionally a Cursor is not modifyable to my knowledge or is it?

所以我想通了,它的工作原理,但pretty坏的编程风格,因为我做双倍的工作解决办法......我只是把同样的数据到另一个容器中,然后使用它。是不是有这种直接的方式?这实际上是一个简单的任务......一定有办法做到这一点,我没有看到它。

So i figured out a workaround which works but is pretty bad programming style, because i'm doing double the work ... i just put the same data into another container and then use it. Isn't there a direct way for this? This is actually a simple task ... there must be a way to do this and i don't see it.

这里的code:

protected void onPrepareDialog(int id, final Dialog dialog)
{
   switch(id)
   {
      case R.layout.database_feed:
         /*get all distinct names in the Database */
         Cursor cursor2 = mDatabase.rawQuery("SELECT DISTINCT nickName FROM highscore_table "+
               "ORDER BY nickName COLLATE NOCASE", null);
         /* I didn't find a simple way to set cursor2 in a 
          * CursorAdapter for the AutoCompleteTextView.
          * this was my first try (does not work): */
         /*((AutoCompleteTextView) dialog.findViewById(R.id.actvName)).setAdapter(
               new SimpleCursorAdapter(this,android.R.layout.simple_dropdown_item_1line, 
                     cursor2, new String[] { "nickName" },
                     new int[] { android.R.layout.simple_dropdown_item_1line } )
         ); */

         /*this is my workaround ... it works but it's horrible*/
         LinkedList<String> llsNames = new LinkedList<String>();
         for(cursor2.moveToFirst(); !cursor2.isAfterLast(); cursor2.moveToNext())
         {
            llsNames.addLast(cursor2.getString(0));
         }
         ((AutoCompleteTextView) dialog.findViewById(R.id.actvName)).setAdapter(new ArrayAdapter<String>(
               this, android.R.layout.simple_dropdown_item_1line, llsNames
         ));
         break;
      default:
         break;
   }
}

感谢您的帮助。

推荐答案

你试过:

SELECT _id, nickName FROM highscore_table GROUP BY nickName ORDER BY nickName COLLATE NOCASE

我不太清楚,如果这个工程SQLite中。

I'm not quite sure if this works in SQLite.

这篇关于SimpleCursorAdapter不会由于缺少_id的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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