我正在开发一个小应用程序,我必须在其中使用带有数据库的微调器。我在用
spinner.setOnItemSelectedListener(this);
loadSpinnerData();
我的 loadSpinnerData() 在下面
private void loadSpinnerData() {
// database handler
AndroidContext.setContext(this);
sqlitedatabase_obj = DatabaseHelper.getInstance().getDb();
// Spinner Drop down elements
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
List<String> lables = db.getAllLabels();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);
// 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);
}
getAllLabels 在 DatabaseHelper 类中定义
public List<String> getAllLabels(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT _id, product_name FROM list_create WHERE _id = ?";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
我想用数据库中的数据显示动态微调器。但是即使我没有得到异常或错误,我也无法从数据库中填充数据。