0

I have a spinner which starts a second activity on a value change. This works good.

After a return everything works fine too. But if I rotate now the mobile the OnItemSelectedListener is called! Why? The spinner should be on position -1. Which means that the callback will not be called.

How does android know which spinner item is selected? I tried to prevend to restore this information by sending the base class the a null value for the savedInstanceState.

Here is some code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    setContentView(R.layout.menu);
    String db=MyDatabase.getData();

    if(db == null)
        db="";
    Spinner spinner=(Spinner)findViewById(R.id.spinner);
    MyAdapter adapter=new MyAdapter(this, db, "ID", "Name");
    spinner.setAdapter(adapter);
    adapter.setHint(spinner, "Please select...");
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id2) {
            int id=((SpinnerItem)parent.getAdapter().getItem(position)).getId();
            Intent intent=new Intent();
            intent.setClass(MainMenu.this, Other.class);
            intent.putExtra("id", id);
            startActivity(intent);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });
}

Here is my adapter:

public class MyAdapter extends ArrayAdapter<SpinnerItem> {
    private final Context context;
    private String hint;

    public MyAdapter(Context context, String input, String key, String value) {
        super(context, android.R.layout.simple_spinner_item, create(input, key, value));
        this.context=context;
    }

    private static SpinnerItem[] create(String input, String key, String value) {
        Vector<SpinnerItem> list=new Vector<SpinnerItem>();
        // fill the list
        return list.toArray(new SpinnerItem[] {});
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // show the hint
        if(position == -1) {
            TextView tv=new TextView(context);
            tv.setTextColor(Color.DKGRAY);
            tv.setText(hint);
            return tv;
        }
        return super.getView(position, convertView, parent);
    }

    public void setHint(Spinner spinner, String hint) {
        if(spinner.getAdapter() == null) {
            throw new IllegalStateException("Set your adapter first!");
        }
        this.hint=hint;
        try {
            final Method m=AdapterView.class.getDeclaredMethod("setNextSelectedPositionInt", int.class);
            m.setAccessible(true);
            m.invoke(spinner, -1);

            final Method n=AdapterView.class.getDeclaredMethod("setSelectedPositionInt", int.class);
            n.setAccessible(true);
            n.invoke(spinner, -1);
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
    }
}
rekire
  • 47,260
  • 30
  • 167
  • 264

2 Answers2

1

When you rotate your device your Activity gets restarted or recreated add this line to your AndroidManifest file in activity tag to avoid it.

android:configChanges="keyboardHidden|orientation"
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
1

When you change the orientation the onCreate() of Activity get Called.

and by default as the behaviour of the spinner the method onItemSelectedListener() get called of the spinner when ever the activity gets created.

So this is happening to you in this case

Solution for such issue is given here StackOverflow Question's Link check how he handled the issue by taking two counters checkBoxCounter and checkBoxInitialized and try to implement same in your case.

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • That could work I'll check that. But how can I reset the spinner on rotation? My initial status with the text please select would be enough. – rekire Oct 19 '11 at 11:32
  • you can store position in variable and then use spinner method sp.setSelection(position); – MKJParekh Oct 19 '11 at 11:36