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?
How to populate a Spinner widget from a database
Follow it, its really simple.
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.
yes, cursor is required to fetch result set object or data as result set. You can find solution from
Android Developers Blog