开发者

Android array list from database

开发者 https://www.devze.com 2023-03-01 12:08 出处:网络
I wanna create an array of cities that are stored in the database Cities Ta开发者_JAVA百科ble CREATE TABLE cities (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);

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

Cities Ta开发者_JAVA百科ble

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消