I am new in android. I need to fill the spinner from SQLite database.
my layout >> main.xml contains
<Spinner
android:id="@+id/cmbLocations"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/location_prompt"
/>
<EditText android:id="@+id/text1" android:layout_width="300dp" android:layout_height="wrap_content"/>
I have a public function which returns the locations:
public static final String KEY_ROWID = "_id";
public static final String KEY_LOCATION = "location";
public Cursor GetLocations()
{
return db.query(DATABASE_TABLE,
new String[]
{
KEY_ROWID, // "_id"
KEY_LOCATION // "location"
}, null, null, null, null, null);
}
The code which is populating the spinner is :
Spinner cmbLocations = (Spinner) findViewById(R.id.cmbLocations);
DBAdapter db = new DBAdapter(this);
db.open();
Cursor cur = db.GetLocations();
String[] from = new String[]{"_id","location"};
int[] to = new int[]{R.id.text1};// I don't know that why text1 is required here
startManagingCursor(cur);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, cur, from, to);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cmbLocations.setAdapter(adapter);
db.close();
The issue i am facing is that the spinner controls shows EMPTY locations. For example if i have two locations, Location1 & Location2. The spinner will show two EMPTY locations. If i have 3 locations the spinner will show three EMPTY locations.
Thanks in advance for your help.
Try using:
int[] tois for placing the values from the cursor,you defined in from.. For example,you are fetching _id and location then you would have to specify its destination,here it might be textview.