Help in choosing a counter selection time

In my Android application, I have a sound that I want to play when a certain choice was made from the counter, but I want it to play when the user really makes the right choice (or right after it). My problem is that, although the sound is played when they make the right choice, while that choice remains selected, it also plays every time the application starts, when it should ONLY play at the time it was selected. Here is the code that I have now:

fitnessSpinner = (Spinner) findViewById(R.id.fitness_spinner); ArrayAdapter adapter4 = ArrayAdapter.createFromResource( this, R.array.fitness_array, android.R.layout.simple_spinner_item); adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); fitnessSpinner.setAdapter(adapter4); fitnessSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long i) { Log.d("test", "p: " + position + " " + i); if(position == 0) { //First Entry MediaPlayer mp = MediaPlayer.create(mContext, R.raw.bowchica); mp.start(); } if(position == 4) { MediaPlayer mp = MediaPlayer.create(mContext, R.raw.debbie2); mp.start(); } } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); 

How to make the sound stop playing when the application starts? Should I wrap the whole fitnessSpinner.setOnItemSelectedListener ... inside some type of change selection, if such a thing exists?

+4
source share
1 answer

I could think of a workaround you could use -

You can use dummy notation in your array. Let's say if you want to place an array of planets in a spinner, then you can have an array like -

 List of Planets Mars Earth Venus .. 

When the application starts, an informative message will be displayed as "List of planets", and in your callback you can have a code, for example -

 if(0 != pos){ //Play a sound } 
0
source

All Articles