我的代码中没有错误。它工作得很好,除了没有按预期从数据库中填充Spinner。相反,它是空的。请帮忙!!
从SQLite中检索记录:
// get all contacts
public List getAllNames() {
List names = new ArrayList();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
names.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
db.close();
// returning contacts
return names;
}
加载微调器:
private void loadSpinnerData() {
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
List<String> contacts = db.getAllNames();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, contacts);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
我想它所需要的只是一个小调整,但我不知道在哪里。提前谢谢!