我正在填充数据库中的微调器
// Populating the City Spinner
Cursor cities = db.cityList();
startManagingCursor(cities);
// create an array to specify which fields we want to display
String[] from = new String[] { DBAdapter.KEY_NAME };
// create an array of the display item we want to bind our data to
int[] to = new int[] { android.R.id.text1 };
Spinner cityList = (Spinner) this.findViewById(R.id.citySpiner);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cities, from, to);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cityList.setAdapter(adapter);
当我尝试从这个
的微调器的选定项目中获取内容时// Get the City
Spinner getCity = (Spinner) findViewById(R.id.citySpiner);
String cityName = getCity.getSelectedItem().toString();
我得到以下内容。 有没有办法可以从数据库中获取城市名称或城市ID。
答案 0 :(得分:8)
我认为,因为您正在使用customadapter并在适配器中提供三个列表...
只需调用getSelectedItem()..
就无法获取所选文本使用此:
Spinner mySpinner = (Spinner)findViewbyId(R.id.spinner);
int position = mySpinner.getSelectedItemPosition();
String Text = yourCityList[position].toString(); // dont know which list is holding the city list...
// i didn't use any db in any of my app so cant tell you how can you get list...
// leaving it to you... :)
希望它有所帮助......
答案 1 :(得分:5)
只需从微调器获取适配器并从光标
获取字符串Cursor cursor = (Cursor) myAdapter.getItem(position);
String cityName = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_NAME));
答案 2 :(得分:1)
答案 3 :(得分:1)
List<String> list = new ArrayList<String>();
s = (Spinner)findViewById(R.id.spinner1);
SQLiteDatabase sqldb = db.openDataBase();
Cursor cr = sqldb.rawQuery("select Name from employee",null);
if (cr.moveToFirst()) {
do {
list.add(cr.getString(0).toString());
Toast.makeText(this,cr.getString(0).toString(),Toast.LENGTH_LONG).show();
ArrayAdapter <String> a= new ArrayAdapter(this, android.R.layout.simple_spinner_item,list);
a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(a);
} while (cr.moveToNext());
}//urvesh patel
答案 4 :(得分:0)
我实现它的方式是这样的:
您必须将getItem()方法强制转换为游标,因为它实际上只是返回游标而不是光标中的单个行,这很烦人。
final Cursor cursor = (Cursor) yourSpinner.getAdapter().getItem( position );
将光标移动到第一行
cursor.moveToFirst();
现在,您可以使用与最初查询的表匹配的列名来获取游标中的字符串,当使用getAdapter()
设置上面的抓取器时final String selectedPartUUID = cursor.getString( cursor.getColumnIndex( TABLE_COLUMN_NAME ) );
希望这会有所帮助,并希望得到任何反馈:)
答案 5 :(得分:0)
希望这会有所帮助!!
Toast.makeText(getApplicationContext(), "getSelectedItem=" + spinnerString, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "getSelectedItemPosition=" + nPos, Toast.LENGTH_SHORT).show();