The Bad Android spinner

I have an android spinner, on which when we change the chosen element in it a listview data changes. But at one moment I decide that i want to  load the data from another component and not from the spinner. I load them up, rotate the phone and the data disappears, despite that i did save them in onSaveInstanceState.

I debugged whole fuckin day and i saw that onItemSelected method of the spinner is also executed together with the start of the Activity in which it is. I found the solution here: http://stackoverflow.com/questions/5624825/spinner-onitemselected-executes-when-it-is-not-suppose-to

public class myActivity extends Activity implements OnItemSelectedListener
{
//this counts how many Gallery's are on the UI
private int mGalleryCount=0;

//this counts how many Gallery's have been initialized
private int mGalleryInitializedCount=0;

//UI reference
private Gallery mGallery;


@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView
(R.layout.myxmllayout);

//get references to UI components
mGallery
= (Gallery) findViewById(R.id.mygallery);

//trap selection events from gallery
mGallery
.setOnItemSelectedListener(this);

//trap only selection when no flinging is taking place
mGallery
.setCallbackDuringFling(false);

//
//do other stuff like load images, setAdapter(), etc
//

//define how many Gallery's are in this view
//note: this could be counted dynamically if you are programmatically creating the view
mGalleryCount
=1;

}


public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{

if (mGalleryInitializedCount < mGalleryCount)
{
mGalleryInitializedCount
++;
}
else
{
//only detect selection events that are not done whilst initializing
Log.i(TAG, "selected item position = " + String.valueOf(position) );
}

}

}

You may also like...