如何将微调器值赋予字符串

时间:2013-08-12 11:17:50

标签: android spinner

无法获取微调器的选定值。

public class Create extends Activity {

    public ArrayList<String> spinnerlist;
    Button button4;
    String spinnertext;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create);
        Spinner spinner = (Spinner) findViewById(R.id.spinner1);

        spinnerlist=new ArrayList<String>();
        spinnerlist.add("ATM");
        spinnerlist.add("Bank");
        spinnerlist.add("Mail");
        spinnerlist.add("Others");
        spinnerlist.add("Personal");
        spinnerlist.add("Social Network");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, spinnerlist);
        adapter.setNotifyOnChange(true);
        spinner.setAdapter(adapter);
        spinner.setLongClickable(true);
        spinner.setOnLongClickListener(new OnLongClickListener(){
            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub
                spinnertext = spinnerlist.**getSelectedItem()**.toString();
                               return false;

获取错误:方法 getSelectedItem()未定义类型ArrayList

       }
        });


        button4 = (Button)findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent5 = new Intent(view.getContext(), Details.class); 
                startActivityForResult(myIntent5, 0);
                myIntent5 .putExtra("key", spinnerlist.getSelectedItem().toString());
                startActivity(myIntent5);
                }
        });

}}

我无法将字符串值传输到下一个活动?

3 个答案:

答案 0 :(得分:4)

您从spinnerlist而不是spinner

获取值
    button4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent5 = new Intent(view.getContext(), Details.class); 
            startActivityForResult(myIntent5, 0);
            myIntent5.putExtra("key", spinner.getSelectedItem().toString());
            startActivity(myIntent5);
            }
    });

您需要将spinnerlist.getSelectedItem().toString()替换为spinner.getSelectedItem().toString()

答案 1 :(得分:0)

使用setOnItemSelectedListener代替onLongClickListener

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            String item =String.ValueOf(parent.getItemAtPosition(pos));
        }
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

button4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent5 = new Intent(view.getContext(), Details.class); 
                startActivityForResult(myIntent5, 0);
                myIntent5 .putExtra("key", item);
                startActivity(myIntent5);
                }
        });

答案 2 :(得分:0)

您还可以通过简单的方式获取微调器的值

Spinner ClassSpinner = (Spinner)findViewById(R.id.Class);
String Class = ClassSpinner .getSelectedItem().toString();