问题
hello I try to put in my java Code a Spinner, but it shows in the else if statement error:
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);
}
}
}
});
}
it shows in the CategorySpinner.getText() an error. I hope you can help me. I´ve tried a lot, but not found a solution.
回答1:
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()));
}
}
回答2:
You haven't provided adapter to spinner. please use below code for adapter:
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
after that if u want text of an item than use below code:
String value = spinnerArray[spinner.getSelectedItemPosition()];
which will return value of selected item in spinner. Default it is 0.
来源:https://stackoverflow.com/questions/17744978/android-spinner-error