-1

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"

silversunhunter
  • 1,219
  • 2
  • 12
  • 32

1 Answers1

0

Do like this that should work.

Toast.makeText(getApplicationContext(), event_spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • What if I want the value of the selected item? Like in HTML you can do – silversunhunter Nov 16 '14 at 19:46
  • 1
    @silversunhunter why you do not check docs first or google? http://stackoverflow.com/questions/1947933/how-to-get-spinner-value – Marcin Orlowski Nov 16 '14 at 19:48
  • @MarcinOrlowski Why do you think I did not? I searched but was not understanding what was wrong. Sorry, but I am brand new to Java. – silversunhunter Nov 16 '14 at 22:56
  • @silversunhunter because link I gave shows up as 1st when you search for "android spinner current value". Java got nothing with this. – Marcin Orlowski Nov 16 '14 at 22:57