如何在android中使用字符串Buffer从Spinner显示所选项目

时间:2015-04-21 09:11:42

标签: android

public class AddCourse extends Activity implements OnClickListener {

    private Spinner addCourse;
    String addedcoursevalue;
    TextView AddcourseButton;
    StringBuffer sb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addcourse);

        addCourse = (Spinner) findViewById(R.id.spinner1);
        AddcourseButton = (TextView) findViewById(R.id.AddcourseButton);
        AddcourseButton.setOnClickListener(this);
        final String[] coursearray = getResources().getStringArray(
                R.array.Course_arrays);

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                R.layout.spinner_item, coursearray);
        dataAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        addCourse.setAdapter(dataAdapter);
        addCourse.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                addedcoursevalue = addCourse.getSelectedItem().toString();
                sb = new StringBuffer();
                sb.append(addedcoursevalue);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Toast.makeText(AddCourse.this, sb, 1000).show();

    }

}

这是我的代码我能够在微调器中显示值我希望我选择的应该存储在缓冲区中我想要打印按钮点击吐司请检查哪里有错误请帮助

1 个答案:

答案 0 :(得分:0)

  

我希望我选择的应该存储在我想要的缓冲区中   打印按钮单击

无需在onItemSelected方法的Buffer中存储Spinner选定的值。您可以通过调用onClick

直接访问addCourse.getSelectedItem()方法中的选定值
    sb = new StringBuffer(); 
    addCourse.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            addedcoursevalue = addCourse.getSelectedItem().toString();
            sb.append(addedcoursevalue);
        }