0

I've got the following minimal program that shows my problem (android 2.2 (API 8)): If I call the start()-Method from onCreate() everything works fine but if I call it from onWindowFocusChanged() I'm not able to select an item from the spinner. Can anybody help me?

package de.thomasklein1982;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class SpinnerExample extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //call start() from here and everything is just fine
        //start();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        //call start() from here and it's impossible to select a spinner-item
        start();
    }

    public void start(){
        Spinner spinner=new Spinner(this);
        List<String> list = new ArrayList<String>();
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getBaseContext(),
                    android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);
        setContentView(spinner);    
    }
}
thomas
  • 1
  • Why are you trying to set the ContentView from onWindowFocusChanged? – Martze Jul 05 '12 at 10:39
  • That's a long story. Is it enough if I tell you, that I have to? – thomas Jul 05 '12 at 10:48
  • I don't want to know your life's story, even it might be interesting. What do you want to happen by setting the ContentView from onWindowFocusChanged? – Martze Jul 05 '12 at 10:51
  • Alright then... I need the actual size of the displayed activity (without status bar and title bar). Therefore in onCreate() I add a button that is maximized and in onWindowFocusChanged I measure the width and height of the button. After that I remove the button and go on. – thomas Jul 05 '12 at 11:03
  • I tried the following: I created a RelativeLayout as ContentView in onCreate() and added the spinner to this Layout in the start()-method. The effect: The spinner-menu now does no longer disappear but it's impossible to select another item than the first one. – thomas Jul 05 '12 at 11:21
  • maybe you could try to insert the spinner from beginning, but set its visibility to gone, and in onwindowsfocuschanged yout set the button to gone, the spinner to visible, and work then with the size of the activity. – Martze Jul 05 '12 at 11:27
  • Hmm, I already thought about that, but the problem is, that during runtime there will be spinners added so I don't know how many spinners will be there. – thomas Jul 09 '12 at 14:52
  • What about creating a listView with a custom adapter, so you can add and delete the spinners? I tried that with a ListActivity and it worked. – Martze Jul 10 '12 at 07:11
  • Okay, I will try that. Sorry, I was in the hospital for a few days. – thomas Jul 15 '12 at 09:39

1 Answers1

1

your start() method is also called when you select any item in the spinner. So it becomes a newly created each time and the first one is selected by default as first in the list.