微调器到字符串的值

时间:2017-04-19 20:40:46

标签: java android spinner android-spinner

正在制作我的第一个应用程序并且进展缓慢。只是想知道我如何返回一个Spinner值以将其作为字符串传递。

这是我的代码:

首先是事件类:

public void event(){
    calanderBtn = (Button)findViewById(R.id.eventBtn);
    calanderBtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_INSERT);
            intent.setType("vnd.android.cursor.item/event");
            intent.putExtra(CalendarContract.Events.TITLE, "Home");
            intent.putExtra(CalendarContract.Events.DESCRIPTION,  "Cleaning : ");
            intent.putExtra(CalendarContract.Events.EVENT_LOCATION, "Home");
            startActivity(intent);
        }
    });
}

第二个Spinner类:

public void selectcleaning() {
        spCleaning = (Spinner) findViewById(spinner);

        adapterCleaningType = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, cleaningType);
        adapterCleaningType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spCleaning.setAdapter(adapterCleaningType);
    }

我正在尝试从selectcleaning中选择值(值为Yes或No)并将其传递给事件类Calendar中的descrption,我不知道该怎么做。

3 个答案:

答案 0 :(得分:0)

你应该尝试使用spinner类的覆盖:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
    // your code here

}});

出于您的目的,'int position'将等于0(是)或1(否)

答案 1 :(得分:0)

尝试设置选定的侦听器。选择项目后,可以将该值存储在全局变量中。它也默认获得第一个项目。

    spCleaning.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            // Value is current spinner item
            globalStringVariable = (String) parent.getItemAtPosition(position);

        }

        // Because AdapterView is an abstract class, onNothingSelected must be defined
        @Override
        public void onNothingSelected(AdapterView<?> parent) {

            // This is default, before you've selected anything. 
            // It gets the first value.
            globalStringVariable = parent.getItemAtPositon(0);
        }

    });

答案 2 :(得分:0)

您可以使用此代码获取值

Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();

或使用OnItemSelectedListener(Android Doc)选择微调器时获取值

public class SpinnerActivity extends Activity implements 

OnItemSelectedListener {
    @Override
    protected void onStart() {
         Spinner spinner = (Spinner) findViewById(R.id.spinner);
         spinner.setOnItemSelectedListener(this);
    }

    public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
}