我有一个spinner加载数据到sqlite
我在数据库中有字段ID和字段名称。
private void loadSpinnerDataHama() {
// database handler
DatabaseSpinner db = new DatabaseSpinner(getApplicationContext());
// Spinner Drop down elements
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
spin2.setAdapter(dataAdapter);
}
public List<String> getAllLabels(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_LABELS;
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;
}
,结果是
美国 - &gt;价值是“美国”
法国 - &gt;价值是“法国”
当我将代码labels.add(cursor.getString(1));
更改为labels.add(cursor.getString(0));
,结果是
1 - &gt;值为“1”
2 - &gt;值为“2”
我尝试使用int position2 = spin2.getSelectedItemPosition()+1;
,但值是微调器的id / position,而不是数据库的id。
如何在微调器上显示字段名称。但值是名称的id
示例:微调器显示:
美国 - &gt;值为“1”
法国 - &gt;值为“2”
BR
亚历
答案 0 :(得分:24)
这很简单,您可以通过以下方式实现/表示类所需的内容:
创建以下类: SpinnerObject
public class SpinnerObject {
private int databaseId;
private String databaseValue;
public SpinnerObject ( int databaseId , String databaseValue ) {
this.databaseId = databaseId;
this.databaseValue = databaseValue;
}
public int getId () {
return databaseId;
}
public String getValue () {
return databaseValue;
}
@Override
public String toString () {
return databaseValue;
}
}
使用此类,您仍然可以使用ArrayAdapter的Android实现(无需实现自己的实现,因为 toString 方法提供了您要显示的字符串值,并且您仍然存储您还需要的数据库ID。
现在您必须按如下方式创建列表:
public List < SpinnerObject> getAllLabels(){
List < SpinnerObject > labels = new ArrayList < SpinnerObject > ();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_LABELS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if ( cursor.moveToFirst () ) {
do {
labels.add ( new SpinnerObject ( cursor.getString(0) , cursor.getString(1) ) );
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning labels
return labels;
}
现在你有了带有值和id的对象列表,你就这样加载了微调器:
private void loadSpinnerDataHama() {
// database handler
DatabaseSpinner db = new DatabaseSpinner(getApplicationContext());
// Spinner Drop down elements
List <SpinnerObject> lables = db.getAllLabels();
// Creating adapter for spinner
ArrayAdapter<SpinnerObject> dataAdapter = new ArrayAdapter<SpinnerObject>(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
spin2.setAdapter(dataAdapter);
}
旋转器将显示值,同时显示其ID(来自数据库)。
要检索当前所选项目的ID,请执行以下操作:
int databaseId = Integer.parseInt ( ( (SpinnerObject) spin2.getSelectedItem () ).getId () );
请尝试一下,让我知道会发生什么。