0

How can I populate a spinner content from database (SQLite)

I have POJO: categories, contain id and name, I have the table already, with a function to get the ArrayList like this:

public List<SetcardCategory> getAllSetcardCategory()
{
    List<SetcardCategory> setcardCategories = new ArrayList<SetcardCategory>();
    String selectQuery = "SELECT  * FROM " + TABLE_SETCARD_CATEGORIES;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
        do {
            SetcardCategory setcardCategory = new SetcardCategory();
            setcardCategory.setId(c.getInt((c.getColumnIndex("id"))));
            setcardCategory.setName(c.getString(c.getColumnIndex("name")));

            // adding to tags list
            setcardCategories.add(setcardCategory);
        } while (c.moveToNext());
    }
    return setcardCategories;
}

Then on Activity I call it like this:

List<SetcardCategory> setcardCategories = db.getAllSetcardCategory();
    ArrayAdapter<SetcardCategory> arrayAdapter = new ArrayAdapter<SetcardCategory>(
            this, android.R.layout.simple_spinner_item, setcardCategories);
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner sItems = (Spinner) findViewById(R.id.setcardCategory);
    sItems.setAdapter(arrayAdapter);

when I run it, it loads string like this: "schema.SetcardCategory@22293c98" and many others values similar to that.

How can I populate the spinner to show the name field as a label, and id field as the value that we fetch to save into DB?

2
  • in the pojo class overide tostring method and return the value you want to display in the spinner Commented Nov 17, 2017 at 6:23
  • you can do that just in 5 lines of code by using SimpleCursorAdapter Commented Nov 17, 2017 at 7:47

2 Answers 2

3
class Pojo{
 private String name;
  @Override
    public String toString() {
        return name;
    }
}

do it like this in the pojo class, so this will return a value for the object when it uses the to string method in the adapter, to load the data

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

Comments

2

Solution 1 Overide the toString method in your SetcardCategory class

class SetcardCategory {
...
...
@Override
    public String toString() {
        return this.name;
    }
}

Solution 2 If you just want to show the name, Just pick name only from DB

public List<String> getAllSetcardCategory()
    {
        List<String> setcardCategories = new ArrayList<String>();
        String selectQuery = "SELECT  * FROM " + TABLE_SETCARD_CATEGORIES;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (c.moveToFirst()) {
            do {
                // adding to tags list
                setcardCategories.add(c.getString(c.getColumnIndex("name")));
            } while (c.moveToNext());
        }
        return setcardCategories;
    }

And create Array Adapter as

List<String> setcardCategories = db.getAllSetcardCategory();
    ArrayAdapter<SetcardCategory> arrayAdapter = new ArrayAdapter<SetcardCategory>(
            this, android.R.layout.simple_spinner_item, setcardCategories);
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

4 Comments

by using this one, i cannot fetch the id, right? Manoj kumar answer fit the situation better
I given both solutions. I have given the second solution because of looks like you only need the name.
oh ya, im so sorry, i saw Manoj answer first, but actually your answer posted first.
@Gabriel no, it does not provide valid row IDs in the adapter, for that you should use CursorAdapter - for example SimpleCursorAdapter

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.