我有一个方法可以获取SQLite表中的所有值。该应用程序使用SQLite。现在,我想在旋转器中加载这些数据。我该怎么做?
public List<Outlet> getAllOutlet(){
List<Outlet> outletlist = new ArrayList<>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_OUTLET;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Outlet outlet = new Outlet();
outlet.setOID(Integer.parseInt(cursor.getString(1)));
outlet.setOutletname((cursor.getString(2)));
outlet.setOCode(cursor.getString(3));
// Adding outlet to list
outletlist.add(outlet);
} while (cursor.moveToNext());
}
// return outlet list
return outletlist;
}
答案 0 :(得分:-2)
您可以通过创建数组适配器来实现,如下所示
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Spinner Drop down elements
List<String> outletNames = new ArrayList<String>;();
List<Outlet> outlets = getAllOutlet();
for(Outlet outlet: outlets){
outletNames.add(outlet.getOutletname);
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, outletNames);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
或者,如果您希望使用自定义视图进行更好的自定义,请创建自己的适配器以扩展ArrayAdapter。 如果遇到问题,也可以按照本教程进行操作 Android populating Spinner data from SQLite database