Android Spinner错误

时间:2013-07-19 11:27:37

标签: android android-spinner

你好我试着在我的java Code中放入一个Spinner,但它在else if语句中显示错误:

    private EditText textName;
    private EditText textContent;
    private Spinner CategorySpinner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);



        configureButton1();
        configureButton2();

        textName = (EditText) findViewById(R.id.editTextName);
        textContent = (EditText) findViewById(R.id.editContent);
        CategorySpinner = (Spinner) findViewById(R.id.editCategorySpinner);



    }

    private void configureButton1() {
        Button del = (Button)findViewById(R.id.btNewText);
        del.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(v.getContext(), StartActivity.class);
                v.getContext().startActivity(myIntent);
            }
        });
    }

    private void configureButton2() {
        Button save = (Button) findViewById(R.id.btSave);

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (textName.getText().length() == 0) {
                    textName.requestFocus();
                    return;
                } else if (textContent.getText().length() == 0) {
                    textContent.requestFocus();
                    return;
                } else if (CategorySpinner.**getText**().length() == 0) {
                    CategorySpinner.requestFocus();
                    return;
                } else {
                    //next Step: get the mood
                    Intent myIntent = new Intent(v.getContext(), Activity2.class);
                myIntent.putExtra("textName", textName.getText().toString());
                myIntent.putExtra("textContent", textContent.getText().toString());
                myIntent.putExtra("CategorySpinner", CategorySpinner.**getText()**.toString());
                v.getContext().startActivity(myIntent);
            }
                }
            }
        });
        }

它在CategorySpinner.getText()中显示错误。我希望你能帮助我。 我尝试了很多,但没有找到解决方案。

2 个答案:

答案 0 :(得分:0)

    private void initSpinnerList() {
    spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(listner);
    categories = new ArrayList<String>();
    categories.add("S");
    categories.add("T");
    categories.add("U");
    categories.add("V");
    categories.add("W");
    categories.add("X");
    categories.add("Y");
    categories.add("Z");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, categories);
    dataAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(dataAdapter);
}

private OnItemSelectedListener listner = new OnItemSelectedListener() {
    /*
     * (non-Javadoc)
     * 
     * @see
     * android.widget.AdapterView.OnItemSelectedListener#onItemSelected(
     * android .widget.AdapterView, android.view.View, int, long)
     */
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {
        String item = parent.getItemAtPosition(position).toString();
        /// on item selected data 
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.widget.AdapterView.OnItemSelectedListener#onNothingSelected
     * (android .widget.AdapterView)
     */
    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
};

@Override
        public void onClick(View v) {
            if (spinner != null && categories != null) {
                Log.v("Selected Spinner Item ",
                        categories.get(spinner.getSelectedItemPosition()));
            }
        }

答案 1 :(得分:0)

您尚未向微调器提供适配器。请使用以下代码获取适配器:

Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);

之后如果你想要一个项目的文本而不是使用下面的代码:

String value = spinnerArray[spinner.getSelectedItemPosition()];

将返回微调器中所选项的值。默认为0。