【发布时间】:2013-04-22 06:36:18
【问题描述】:
我希望有人可以帮助我找出为什么以下代码不适用于我。我有一种从数据库表中获取所有名称的方法,我使用 List 来获取它们。这是我获取它们的代码
/*fetch all names to spinner*/
public List<String> readAllNames()
{
List<String> names = new ArrayList<String>();
String[] allNames = {SQLiteHelper.Names};
String selection = SQLiteHelper.Names + " LIKE ?";
String[] selectionArgs = null;
cursor = database.query(SQLiteHelper.LECTURE_NAME, allNames, selection, selectionArgs, null, null, null);
cursor.moveToFirst();
while(!cursor.isAfterLast())
{
names.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Names)));
cursor.moveToNext();
}
cursor.close();
return names;
}
这是我调用该方法并填充我的微调器的代码
datasource.openToRead();
List<String> names = datasource.readAllNames();
ArrayAdapter<String> spin = new ArrayAdapter<String>(Home.this, android.R.layout.simple_spinner_item, names);
spin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ins_jadname.setAdapter(spin);
datasource.close();
问题是微调器是空的,大家知道我的代码有什么问题吗?我不想使用简单的光标适配器,因为我认为更简单。 还是谢谢。
【问题讨论】:
-
查询数据库后,你有没有测试过名字列表是否有值?
-
还没有,如何查看?
-
只需使用
Log行:Log.e("LOOK_FOR_ME", "The size of the list is " + names.size());(在readAllNames()方法中关闭Cursor后插入。然后在Logcat 中查看列表的大小。跨度> -
结果是 0,你知道如何修复我的代码吗?
-
谢谢@Luksprog 我已经解决了这个问题,我使用 rawQuery 方法来查询我的数据。
标签: android database sqlite spinner android-spinner