Android Spinner 所选项目

标签 android spinner android-cursor

我正在像这样从数据库中填充一个微调器

    // 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。

enter image description here

最佳答案

我认为,当您使用自定义适配器并在适配器中提供三个列表时...

你不能简单地通过调用 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... :)

希望对你有帮助....

关于Android Spinner 所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5818850/

相关文章:

android - 按下后退按钮时显示 toast 但 ondestroy() 不工作

java - Open Gl ES 2.0 三角形始终为黑色

java - 如何以这种特殊方式在 Android 中使用微调器?

android - 如何为 Spinner 实现 MatrixCursor?

Android Cursor - moveToNext() 似乎没有到达光标的末尾

android - 无法引用 com.facebook.widget.LoginButton

android - SVN 连接的项目在 Eclipse 中不显示版本信息

Android:当用户在 View 之外按下时如何关闭自定义 View

android - 如何将超过 1 MB 的数据从 sqlite db 加载到 android 游标?

android - 使用新的 Loader API 时如何保持 ListView 的位置?