我有一个包含名称值的自定义微调器。使用此我可以显示一个名称,在后面我可以获得该名称的ID。它工作得很好。看起来像这样 -
// Get the readable version
SQLiteDatabase db = myDbHelper.getReadableDatabase();
Cursor cursor = db.query("address", new String[] { "name", "code"}, null, null, null, null, null);
int noOfRow = cursor.getCount();
Log.v("TAG", "no of data: " + noOfRow);
MyData[] items = new MyData[noOfRow];
int i = 0;
if (cursor.moveToFirst())
{
do
{
// Log.w("TAG", "Name: " + cursor.getString(0));
// Log.w("TAG", "Code: " + cursor.getString(1));
items[i] = new MyData( cursor.getString(0),cursor.getString(1));
i++;
}while (cursor.moveToNext());
StreetSpinnerAdapter = new ArrayAdapter<MyData>( this,android.R.layout.simple_spinner_item,items );
StreetSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SpinnerStreetName.setAdapter(StreetSpinnerAdapter);
populateSpinnerData(items);
}
cursor.close();
db.close();
class MyData
{
public MyData( String spinnerText, String value )
{
this.spinnerText = spinnerText;
this.value = value;
}
public String getSpinnerText() {
return spinnerText;
}
public String getValue() {
return value;
}
public String toString() {
return spinnerText;
}
String spinnerText;
String value;
}
我正在使用代码选择spinner值,这样就像这样 SpinnerStreetName.setSelection(((ArrayAdapter)SpinnerStreetName.getAdapter()).getPosition(SpinnerStreetName.getSelectedItem()));
但它不起作用......
答案 0 :(得分:0)
首先,你必须存储spinner项的值:
String str = spinner.getSelectedItem().toString();
然后就在for循环中执行此操作:
for(int i = 0;i<items.length();i++){
{
if(str.equals(items[i].getSpinnerText()){
//Do with your id by items[i].getValue()
}
}
希望它会帮助你。任何进一步的查询都会问我.. 谢谢。