91

我有 5 个微调器。为了使它总结到这一点。

这是 xml 中的微调器

<Spinner
            android:id="@+id/text_interested"
            android:layout_span="2"
            android:layout_width="wrap_content"
            android:layout_height="60px"
            android:entries="@array/interestedarrays"
            android:prompt="@string/interestedprompt" />

这是 Java 中的 Spinner

submitbtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
interested.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        public void onItemSelected(
                                AdapterView<?> adapterView, View view,
                                int i, long l) {
                            interesting = interested.getItemAtPosition(i).toString();
                        }

                        public void onNothingSelected(
                                AdapterView<?> adapterView) {

                        }
                    });
    }
});

这里的解释:

该页面有一个按钮。按下此按钮将从微调器中读取数据。我用这个检查了输出

System.out.println(interested.getItemAtPosition(i).toString());

它甚至没有给我任何东西。

如何检索值并将其字符串化?

4

14 回答 14

247

尝试这个:

String text = mySpinner.getSelectedItem().toString();

像这样,您可以获得不同 Spinner 的价值。

于 2012-04-26T10:58:39.210 回答
31
String Text = mySpinner.getSelectedItem().toString();

或者

mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Object item = parent.getItemAtPosition(position);
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});
于 2014-03-05T17:59:08.097 回答
12

您可以通过使用从 Spinner 获取所选项目,

interested.getSelectedItem().toString();
于 2012-04-26T10:49:44.030 回答
7

try this

 final Spinner cardStatusSpinner1 = (Spinner) findViewById(R.id.text_interested);
    String cardStatusString;
    cardStatusSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent,
                View view, int pos, long id) {
            cardStatusString = parent.getItemAtPosition(pos).toString();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

 final Button saveBtn = (Button) findViewById(R.id.save_button);
    saveBtn .setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            System.out.println("Selected cardStatusString : " + cardStatusString ); //this will print the result
        }
    });
于 2012-04-26T11:14:57.240 回答
7

如果您的 Spinner 由 SQLite 游标填充,则解决方案是:

Spinner mySpin = (Spinner) findViewById(R.id.myspin);
mySpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
           SQLiteCursor item = (SQLiteCursor) parent.getItemAtPosition(position);
           String value = String.valueOf(item.getString(0));
           Toast.makeText(getApplicationContext(), "The option is:" + value , Toast.LENGTH_SHORT).show(); 
 }

PS: In item.getString(0)-> 0 是您要获取的光标上列的索引。

于 2015-05-28T13:53:39.730 回答
7

使用 Kotlin 获取所选项目:

spinner.selectedItem.toString()
于 2019-09-04T17:33:46.230 回答
5

In addition to the suggested,

String Text = mySpinner.getSelectedItem().toString();

You can do,

String Text = String.valueOf(mySpinner.getSelectedItem());
于 2016-07-16T05:09:48.763 回答
4

由于 Android 开发的最新语言是 Kotlin。下面是我们如何在 Kotlin 中使用匿名对象来做到这一点。

spinnerName?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
    override fun onNothingSelected(parent: AdapterView<*>?) {
       println("Nothing Selected")
    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
       val selectedString = yourList[position]
    }

}
于 2018-06-03T18:18:28.560 回答
3

我认为您希望单击按钮时选择微调器的项目..

尝试getSelectedItem()

spinner.getSelectedItem()
于 2012-04-26T10:57:27.520 回答
3

当您从微调器中选择任何值时,您将获得选定的值,

interested.getSelectedItem().toString();
于 2014-12-11T07:42:29.490 回答
2

尝试这个

sp1 = String.valueOf(spinner.getSelectedItem());
于 2018-02-08T19:00:01.287 回答
2

最好的方法是:-

String selectedItem = spinner.getSelectedItem().toString();

您可以在此处参考文档:Spinners

于 2018-02-08T06:00:42.533 回答
1

通过为您的适配器对象实现 SpinnerAdapter 我使用interested.getItem(i).toString()

于 2012-04-26T10:53:34.183 回答
0
    spinnerType = (AppCompatSpinner) findViewById(R.id.account_type);
    spinnerType.setPrompt("Select Type");

    spinnerType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            TypeItem clickedItem = (TypeItem) parent.getItemAtPosition(position);
            String TypeName = clickedItem.getTypeName();
            Toast.makeText(AddAccount.this, TypeName + " selected", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
于 2020-03-24T19:18:20.710 回答