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

1
  • You need Distinct Names from your table? Commented Jan 13, 2012 at 14:32

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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).
I see, so only thing I have to change is SimpleCursorAdapter to something like: ArrayAdapter<String> new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, from ); and get rid of _id in the sql request ?
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); }
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.

1 Comment

hmm yes I was worried that I would have to create another table. well I can try that as soon as I get home.
0

Maybe You can use DISTINCT in sqlite query: SELECT manual

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.