我在使用Android Lists以及如何将它们转换为在Spinner中使用。将一个字符串数组放入一个Spinner非常简单,因此,我认为对List做同样的处理也很简单。但是在这个时候,我无法弄清楚如何让List成为与Spinner的ArrayAdapter一起使用的正确格式。我有一个帐户名称和一个微调列表,我如何使用列表填充微调?
这是我从数据库中抓取帐户名称的列表代码:
//---retrieves all the accounts matching the account_type---
public List getAccounts(String account_type) {
List<String> list = new ArrayList<String>();
Cursor cursor = this.db.query(DBACCOUNTS, new String[] {
ID,
ACCOUNTTYPE,
ACCOUNTNUMBER,
ACCOUNTNAME},
ACCOUNTTYPE + " = " + "'" + account_type + "'",
null,
null,
null,
null,
null);
if (cursor.moveToFirst()) {
do {
//---account_name column number is 3---
list.add(cursor.getString(3));
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return list;
}
在返回的“名单”,有什么事情我需要做来填充我的微调?下面的代码显然是一个字符串数组,但是,我失去了我需要做什么才能使List与类似的功能一起工作。这是我的非工作ArrayAdapter代码(account_name_array被设置为返回“名单”,从上面):
account_name_spinner = (Spinner) findViewById(R.id.account_name_spinner);
account_name_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, account_name_array);
account_name_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
account_name_spinner.setAdapter(account_name_adapter);
我知道我的方式偏离了轨道在这里,我知道ArrayAdapter期待一个字符串数组,但是,就像我说的,我需要朝着正确的方向努力。很显然,我需要将List转换为字符串数组,或者改变我将Spinner调整为List的方式。 Android对我来说很难掌握,有很多数据结构和更多的数据类型规则,比我用来从PHP背景获得的更多。
非常感谢您的帮助!
这是显示错误吗? –