1

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

VladoPortos
  • 563
  • 5
  • 21

3 Answers3

1

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

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
  • So I should do something like this ? When it comes time to attach the data to an adapter, I like to use a table name alias to query the id field as _id. Example: SELECT id _id, msg from message order by id. That way the adapter sees a field called _id and everybody's happy. – VladoPortos Jan 13 '12 at 14:42
  • When you use ArrayAdapter instead of SimpleCursorAdapter, you don't need _id. You will just construct the cursor data as array and pass that array to adapter (There is overhead of creating array, but we achieve what we want). – kosa Jan 13 '12 at 14:47
  • I see, so only thing I have to change is SimpleCursorAdapter to something like: ArrayAdapter new ArrayAdapter(this,android.R.layout.simple_list_item_1, from ); and get rid of _id in the sql request ? – VladoPortos Jan 13 '12 at 14:53
  • 1
    Cursor nameCursor = db.getNames(); startManagingCursor(nameCursor); //Here Iterate the cursor and get your data and populate String array. String[] from = new String[]{"Name"}; int[] to = new int[]{android.R.id.text1}; ArrayAdapter nameAdapter = new ArrayAdapter (this, android.R.layout.simple_spinner_item, passThestringarrayconstructedAbove); nameSpinner.setAdapter(nameAdapter); } – kosa Jan 13 '12 at 14:57
0

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 have dataId | nameId | time | .... Then you just load up your Spinner with the data from the new nameId | name table.

dmon
  • 30,048
  • 8
  • 87
  • 96
0

Maybe You can use DISTINCT in sqlite query: SELECT manual

Denis Babak
  • 1,874
  • 2
  • 16
  • 13