Reputation: 47
I have a spinner with items mg, g, micrograms, kg. If I select mg, I want the 2nd spinner updated with mg and g items only. But when the use the if logic I get " The method createFromResource(Context, int, int) in the type ArrayAdapter is not applicable for the argume" error.
public class MyOnItemSelectedListener implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
String str = parent.getItemAtPosition(pos).toString();
final TextView result = (TextView) findViewById(R.id.textView5);
if (str.equals("mg")){
Spinner spinner1 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.units, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
//result.setText("testing");
}
else {
result.setText("micrograms");
}
}
@Override
public void onNothingSelected(AdapterView parent){}
}
Upvotes: 0
Views: 212
Reputation: 32690
Your problem is on this line:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.units, android.R.layout.simple_spinner_item);
The first argument here takes a Context. Because you're calling it from a listener, which is its own class, this
refers to the listener, not to your activity. You need to pass a reference to your activity in through the constructor of your listener class, and replace this
with a reference to that activity context.
Upvotes: 1