本文介绍了Android Spinner选定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这样的数据库填充一个微调框

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);

当我尝试从这个微调器的选定项目获得内容

When i try to get the content from the selected item of the spinner like this

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

这篇关于Android Spinner选定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:38