从数据库的Andr​​oid数组列表 [英] Android array list from database

查看:155
本文介绍了从数据库的Andr​​oid数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建存储在数据库中的城市的阵列

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

城市表

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

在查询城市列表

// 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.

推荐答案

尝试 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);

编辑:

是指从光标将被用于显示在文本阵列列(S)微调
表示,这将认为列的值的视图的ID(S)。
这是非常有趣的是,在索引视图的 N 将保存从列文的 N

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.

这篇关于从数据库的Andr​​oid数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆