如何将Spinner选中的项值变为字符串?

Ala*_*Lai 74 android android-spinner

我有5个Spinners.为了对此进行总结.

这是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" />
Run Code Online (Sandbox Code Playgroud)

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

                        }
                    });
    }
});
Run Code Online (Sandbox Code Playgroud)

这里的解释:

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

System.out.println(interested.getItemAtPosition(i).toString());
Run Code Online (Sandbox Code Playgroud)

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

如何检索值并将其串起来?

Bha*_*vin 200

试试这个:

String text = mySpinner.getSelectedItem().toString();
Run Code Online (Sandbox Code Playgroud)

像这样你可以获得不同的Spinners的价值.


小智 25

String Text = mySpinner.getSelectedItem().toString();
Run Code Online (Sandbox Code Playgroud)

要么

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) {
    }
});
Run Code Online (Sandbox Code Playgroud)


Lal*_*ani 11

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

interested.getSelectedItem().toString();
Run Code Online (Sandbox Code Playgroud)


sus*_*ush 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
        }
    });
Run Code Online (Sandbox Code Playgroud)


小智 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(); 
 }
Run Code Online (Sandbox Code Playgroud)

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


Wes*_*y92 6

使用 Kotlin 获取所选项目:

spinner.selectedItem.toString()
Run Code Online (Sandbox Code Playgroud)


Max*_*Max 5

除了建议之外,

String Text = mySpinner.getSelectedItem().toString();
Run Code Online (Sandbox Code Playgroud)

你可以做,

String Text = String.valueOf(mySpinner.getSelectedItem());
Run Code Online (Sandbox Code Playgroud)


Han*_*nny 5

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

}
Run Code Online (Sandbox Code Playgroud)