-1

您好,我尝试在我的 java 代码中放入一个 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() 中显示一个错误。我希望你能帮助我。我已经尝试了很多,但没有找到解决方案。

4

2 回答 2

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()));
            }
        }
于 2013-07-19T11:54:34.247 回答
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。

于 2013-07-19T11:58:12.963 回答