Rahul Baradia
Rahul Baradia

Reputation: 11951

How to get the values from DB in spinner

How to get values into the spinner from the DB. I want to display values or datas on UI from DB. Is it cursor necessary to use to populate the values onto the UI?

Upvotes: 2

Views: 4921

Answers (3)

Riddhish.Chaudhari
Riddhish.Chaudhari

Reputation: 853

yes, cursor is required to fetch result set object or data as result set. You can find solution from

Android Developers Blog

Upvotes: 2

Praveenkumar
Praveenkumar

Reputation: 24476

I just follow the things to get the values from database, when the spinner values changed by postion -

Java class

Spinner spinner = (Spinner) findViewById(R.id.spinner1);
//set some adapter here
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

public class MyOnItemSelectedListener implements OnItemSelectedListener 
{
@SuppressWarnings("unused")
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
try
    {
    switch(parent.getId())
    {
    case R.id.spinner2:

        int selected_spinner_item_position=pos;

        String expired_spinner_item =parent.getItemAtPosition(pos).toString();
        if(selected_spinner_item_position==0)
        {
            //am just using simple cursor adapter here and getting the values from database using dh.fetchAll() when the position is 0
            sc_adapter(dh.fetchAll());
            spinnertemp = selected_spinner_item_position;
        }
        }
    }
}
}

Database

public Cursor fetchAll()
{
    SQLiteDatabase db = this.getReadableDatabase();
    cursor = db.query(t1, new String[] {"_id",task, pname, prior, time, dateformat, duptitle,dupname}, "stat=0", null, null, null, prior);
    return cursor;
}

Just do what you need like these steps.

Upvotes: 2

Yaqub Ahmad
Yaqub Ahmad

Reputation: 27659

How to populate a Spinner widget from a database

Follow it, its really simple.

Upvotes: 2

Related Questions