如何将微调框选择的项目值转换为字符串?


80

我有5个微调框。为了对此进行总结。

这是xml中的Spinner

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

它什么也没有给我,甚至没有。

如何获取值并将其字符串化?


我敢肯定,每当用户单击按钮时,您都希望从微调器中选择项目
Paresh Mayani

在此按钮的onClick外部设置setOnItemSelectedListener,在Onclick方法内部使用此interest.getSelectedItem()。toString();
KK_07k11A0585'4

谢谢,我的错误是将监听器放在按钮监听器中,为什么没有响应
Alan Lai

1
欢迎:)探索api演示...您可以在那里找到更好的示例
KK_07k11A0585 2012年

Answers:


221

试试这个:

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

这样,您可以为不同的锭床匠获得价值。


27
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) {
    }
});

但是如何获得商品0?
Alok Rajasukumaran'7


6

试试这个

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

6

如果您的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:item.getString(0)-> 0是要获取的光标所在列的索引。


5

除了建议之外,

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

你可以做,

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


3

当您从微调器中选择任何值时,就会得到选择的值,

interested.getSelectedItem().toString();




2

由于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]
    }

}

这里没人问科特林。
user3099225


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

        }
    });
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.