1

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

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

}}

我无法将字符串值转移到下一个活动?

4

3 回答 3

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

于 2013-08-12T11:21:40.977 回答
0

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

Spinner ClassSpinner = (Spinner)findViewById(R.id.Class);
String Class = ClassSpinner .getSelectedItem().toString();
于 2013-08-12T11:22:49.700 回答
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);
                }
        });
于 2013-08-12T11:20:05.917 回答