I am creating a spinner based on a webservice call where the items are built in a loop.
Before initiating the loop I wanted to add a default "Select Item" item but when I do I get a fatal exception:
FATAL EXCEPTION: main java.lang.ClassCastException: com.anyscreeninc.posterviewer.Events cannot be cast to java.lang.CharSequence
if (status.contentEquals("complete")){
final Spinner event_spinner = (Spinner)findViewById(R.id.events);
EventAdapter eAdapter = new EventAdapter(posterSessionSetup.this, android.R.layout.simple_spinner_dropdown_item, eventList);
event_spinner.setAdapter(eAdapter);
//When I add this listener I get the error....
//============================================
event_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
Toast.makeText(getApplicationContext(), (CharSequence) event_spinner.getSelectedItem(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView arg0) {
Toast.makeText(getApplicationContext(), "Nothing selected", Toast.LENGTH_SHORT).show();
}
});
}
This is my modified EventAdapter
public class EventAdapter extends ArrayAdapter<Events> {
private Activity context;
ArrayList<Events> data = null;
public EventAdapter(Activity context, int resource,
ArrayList<Events> data) {
super(context, resource, data);
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
}
I am building this stuff based on several differnt tutorial.
I tried to change the (CharSequence) in the Listener to (ArrayList) but AndroidStudio tells me "can't resolve method"