1

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

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

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

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?

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.

Here's the 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;
   }
}

Thanks for any help

Michi
  • 681
  • 1
  • 7
  • 25

2 Answers2

2

Have you tried:

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

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

flo
  • 1,988
  • 12
  • 15
  • Thank you, this actually solves the _id problem. But now i noticed that i have to implement the filtering function of the AutoCompleteTextView because a SimpleCursorAdapter can't do that on its own. So i think i will stick to the workaround solution anyway. But thanks for the hint! By the way: here is a way to change the display of a Cursor: [Google Groups](http://groups.google.com/group/android-developers/browse_thread/thread/796342d2a30c0c0b?pli=1) – Michi Mar 22 '12 at 08:42
0

You can write this way also

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

give first field personid as alias (rename) _id

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Pranita Patil
  • 791
  • 1
  • 9
  • 16
  • I don't understand what you're meaning by "give first field personid as alias (rename)_id" ... How should this help me? How is this answer different to flo's? – Michi May 25 '12 at 09:26
  • if you are using existing database and in that database you have fixed column name then you can use same name using alias _id.no need to change column name for every table in database – Pranita Patil May 28 '12 at 04:34
  • 1
    Ah, now i see what you mean. You didn't quite get the problem. There is already a field _id in that database. But i didn't want to select that column. But android requires me to select that column because otherwise the api can't process it properly. – Michi May 29 '12 at 06:40