0

I wanna create an array of cities that are stored in the database

Cities Table

CREATE TABLE cities (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);

Querying for City List

// City List
public Cursor cityList() throws SQLException {
    return db.query(TABLE_CITIES, new String[] {ID, KEY_NAME}, null, null, null, null, null, null);
}

Trying to Get the content into the Array

    Cursor cities = db.cityList();
    startManagingCursor(cities);

    String[] city_list = new String[] { DBAdapter.KEY_NAME };
    Spinner cityList = (Spinner)this.findViewById(R.id.citySpiner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, city_list);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    cityList.setAdapter(adapter);

Am not able to populate the Spinner.. with the database content.

2
  • hi harsha did you get any error while executing this... Also check whether the cities cursor has some content in it.. Commented Apr 28, 2011 at 11:39
  • dinash.. no it doesnt have. it just had the column name :( Commented Apr 28, 2011 at 11:54

1 Answer 1

1

Try SimpleCursorAdapter

String[] from = new String[] {  DBAdapter.KEY_NAME  };
int[] to = new int[] {android.R.id.text1};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context, android.R.layout.simple_spinner_dropdown_item , cursor, from, to);
cityList.setAdapter(cursorAdapter);

Edit:

from means the column(s) from the Cursor which will be used to display as text array in Spinner. to means the id(s) of the view which will hold the value of that column. It is very interesting that the view at index N will hold the text from column at N.

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

2 Comments

can u explain to me what is String from and int[] to ?
Thanks for the explanation buddy :) its a little confusing but got the idea :)

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.