To view a result of mathematical action
I'd like to show the text containing result of addition of two values: one from onespinner selected item, and another from twospinner selected item. But eclipse shows an error in line
text.setText(onespinner.getSelectedItem + twospinner.getSelectedItem);
What's the matter? Full code goes below.
public class photographer extends Activity implements OnItemSelectedListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner onespinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> unitadapter = ArrayAdapter.createFromResource(
this, R.array.onespinner, android.R.layout.simple_spinner_item);
unitadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
onespinner.setAdapter(unitadapter);
onespinner.setOnItemSelectedListener(this);
Spinner twospinner = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> courseadapter = ArrayAdapter.createFromResource(
this, R.array.twospinner, android.R.layout.simple_spinner_item);
courseadapter.setDropDownViewResource(android.R.layout.simple_开发者_开发百科spinner_dropdown_item);
twospinner.setAdapter(courseadapter);
twospinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
TextView text = (TextView)findViewById(R.id.result);
text.setText(onespinner.getSelectedItem + twospinner.getSelectedItem);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
getSelectedItem
is a method, but you are referencing it like an instance variable. You need to change your code to:
text.setText(onespinner.getSelectedItem() + twospinner.getSelectedItem());
You haven't posted the exact error, but I'm guessing that your problem is that you are trying to make a reference to onespinner
and twospinner
in onItemSelected
and those two objects are not in the scope of that function; they are declared in onCreate
.
Now, the View view
argument of onItemSelected
is the spinner that was clicked, but you need a reference to both spinners (not just the one that was selected). Easiest way to do this is to globally declare oneSpinner
and twoSpinner
and that should solve your problem.
EDIT: Also what Ted Hopp said.
精彩评论