Android的微调所选项目
问题描述:
我是从这样的数据库填充微调
I am populating a spinner from the database like this
// 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。
i get the following. Is there a way i can get the city name or the city id from the database.
答
我想,当你使用的是customadapter并给予三个列表适配器...
I think, as you are using a customadapter and giving three lists in adapter...
您不能简单地通过调用getSelectedItem()获取选中的文本。
you can't get the selected text simply by calling the 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... :)
希望它可以帮助...
Hope it helps....