Android Spinner : Avoid onItemSelected calls during initialization - android

I created an Android application with a Spinner and a TextView. I want to display the selected item from the Spinner's drop down list in the TextView. I implemented the Spinner in the onCreate method so when I'm running the program, it shows a value in the TextView (before selecting an item from the drop down list).
I want to show the value in the TextView only after selecting an item from the drop down list. How do I do this?
Here is my code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class GPACal01Activity extends Activity implements OnItemSelectedListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.noOfSubjects);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.noofsubjects_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id) {
TextView textView = (TextView) findViewById(R.id.textView1);
String str = (String) parent.getItemAtPosition(pos);
textView.setText(str);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}

spinner.setOnItemSelectedListener(this); // Will call onItemSelected() Listener.
So first time handle this with any Integer value
Example:
Initially Take int check = 0;
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id) {
if(++check > 1) {
TextView textView = (TextView) findViewById(R.id.textView1);
String str = (String) parent.getItemAtPosition(pos);
textView.setText(str);
}
}
You can do it with boolean value and also by checking current and previous positions. See here

Just put this line before setting the OnItemSelectedListener
spinner.setSelection(0,false)
This works because setSelection(int, boolean) calls setSelectionInt() internally so that when the listener is added, the item is already selected.
Beware that setSelection(int) won't work, because it calls setNextSelectedPositionInt() internally.

Beginning with API level 3 you can use onUserInteraction() on an Activity with a boolean to determine if the user is interacting with the device.
http://developer.android.com/reference/android/app/Activity.html#onUserInteraction()
#Override
public void onUserInteraction() {
super.onUserInteraction();
userIsInteracting = true;
}
As a field on the Activity I have:
private boolean userIsInteracting;
Finally, my spinner:
mSpinnerView.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View view, int position, long arg3) {
spinnerAdapter.setmPreviousSelectedIndex(position);
if (userIsInteracting) {
updateGUI();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
As you come and go through the activity the boolean is reset to false. Works like a charm.

This worked for me
Spinner's initialization in Android is problematic sometimes
the above problem was solved by this pattern.
Spinner.setAdapter();
Spinner.setSelected(false); // must
Spinner.setSelection(0,true); //must
Spinner.setonItemSelectedListener(this);
Setting adapter should be first part and onItemSelectedListener(this) will be last when initializing a spinner. By the pattern above my OnItemSelected() is not called during initialization of spinner

haha...I have the same question.
When initViews() just do like this.The sequence is the key, listener is the last. Good Luck !
spinner.setAdapter(adapter);
spinner.setSelection(position);
spinner.setOnItemSelectedListener(listener);

To avoid calling spinner.setOnItemSelectedListener() during initialization
spinner.setSelection(Adapter.NO_SELECTION, true); //Add this line before setting listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

My solution:
protected boolean inhibit_spinner = true;
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
if (inhibit_spinner) {
inhibit_spinner = false;
}else {
if (getDataTask != null) getDataTask.cancel(true);
updateData();
}
}

You can do this by this way:
AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//set the text of TextView
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
yourSpinner.setOnItemSelectedListener(listener);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
At first I create a listener and attributed to a variable callback; then i create a second listener anonymous and when this is called at a first time, this change the listener
=]

The user interaction flag can then be set to true in the onTouch method and reset in onItemSelected() once the selection change has been handled. I prefer this solution because the user interaction flag is handled exclusively for the spinner, and not for other views in the activity that may affect the desired behavior.
In code:
Create your listener for the spinner:
public class SpinnerInteractionListener implements AdapterView.OnItemSelectedListener, View.OnTouchListener {
boolean userSelect = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
userSelect = true;
return false;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (userSelect) {
userSelect = false;
// Your selection handling code here
}
}
}
Add the listener to the spinner as both an OnItemSelectedListener and an OnTouchListener:
SpinnerInteractionListener listener = new SpinnerInteractionListener();
mSpinnerView.setOnTouchListener(listener);
mSpinnerView.setOnItemSelectedListener(listener);

create a boolean field
private boolean inispinner;
inside oncreate of the activity
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (!inispinner) {
inispinner = true;
return;
}
//do your work here
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Similar simple solution that enables multiple spinners is to put the AdapterView in a collection - in the Activities superclass - on first execution of onItemSelected(...) Then check to see if the AdapterView is in the collection before executing it. This enables one set of methods in the superclass and supports multiple AdapterViews and therefor multiple spinners.
Superclass ...
private Collection<AdapterView> AdapterViewCollection = new ArrayList<AdapterView>();
protected boolean firstTimeThrough(AdapterView parent) {
boolean firstTimeThrough = ! AdapterViewCollection.contains(parent);
if (firstTimeThrough) {
AdapterViewCollection.add(parent);
}
return firstTimeThrough;
}
Subclass ...
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (! firstTimeThrough(parent)) {
String value = safeString(parent.getItemAtPosition(pos).toString());
String extraMessage = EXTRA_MESSAGE;
Intent sharedPreferencesDisplayIntent = new Intent(SharedPreferencesSelectionActivity.this,SharedPreferencesDisplayActivity.class);
sharedPreferencesDisplayIntent.putExtra(extraMessage,value);
startActivity(sharedPreferencesDisplayIntent);
}
// don't execute the above code if its the first time through
// do to onItemSelected being called during view initialization.
}

Try this
spinner.postDelayed(new Runnable() {
#Override
public void run() {
addListeners();
}
}, 1000);.o

Code
spinner.setOnTouchListener(new View.OnTouchListener() {
#Override public boolean onTouch(View view, MotionEvent motionEvent) { isSpinnerTouch=true; return false; }});
holder.spinner_from.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int slot_position, long l) {
if(isSpinnerTouch)
{
Log.d("spinner_from", "spinner_from");
spinnerItemClickListener.onSpinnerItemClickListener(position, slot_position, AppConstant.FROM_SLOT_ONCLICK_CODE);
}
else {
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});

You could achieve it by setOnTouchListener first then setOnItemSelectedListener in onTouch
#Override
public boolean onTouch(final View view, final MotionEvent event) {
view.setOnItemSelectedListener(this)
return false;
}

This worked for me:
spinner.setSelection(0, false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
spinner.setOnItemSelectedListener(listener);
}, 500);

Based on Abhi's answer i made this simple listener
class SpinnerListener constructor(private val onItemSelected: (position: Int) -> Unit) : AdapterView.OnItemSelectedListener {
private var selectionCount = 0
override fun onNothingSelected(parent: AdapterView<*>?) {
//no op
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
if (selectionCount++ > 1) {
onItemSelected(position)
}
}
}

You can create custom OnItemSelectedListener like this. I've taken val check=0 and in onItemSelected() method i did check if its count is 0? If 0 means its called during initialization. So simply ignore it.
I've also called separate abstract method called onUserItemSelected() I'll call this method is check > 0. This works perfectly fine for me.
abstract class MySpinnerItemSelectionListener : AdapterView.OnItemSelectedListener {
abstract fun onUserItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
private var check = 0
override fun onItemSelected(
parent: AdapterView<*>?,
view: View,
position: Int,
id: Long
) {
if (++check > 1) {
onUserItemSelected(parent, view, position, id)
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
And then you can set listener like this.
mySpinner.onItemSelectedListener = object : MySpinnerItemSelectionListener() {
override fun onUserItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
//your user selection spinner code goes here
}
}

Had the same problem and this works for me:
I have 2 spinners and I update them during init and during interactions with other controls or after getting data from the server.
Here is my template:
public class MyClass extends <Activity/Fragment/Whatever> implements Spinner.OnItemSelectedListener {
private void removeSpinnersListeners() {
spn1.setOnItemSelectedListener(null);
spn2.setOnItemSelectedListener(null);
}
private void setSpinnersListeners() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
spn1.setOnItemSelectedListener(MyClass.this);
spn2.setOnItemSelectedListener(MyClass.this);
}
}, 1);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Your code here
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
When the class is initiating use setSpinnersListeners() instead of directly setting the listener.
The Runnable will prevent the spinner from firing onItemSelected right after the you set their values.
If you need to update the spinner (after a server call etc.) use removeSpinnersListeners() right before your update lines, and setSpinnersListeners() right after the update lines. This will prevent onItemSelected from firing after the update.

For me, Abhi's solution works great up to Api level 27.
But it seems that from Api level 28 and upwards, onItemSelected() is not called when listener is set, which means onItemSelected() is never called.
Therefore, I added a short if-statement to check Api level:
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id) {
if(Build.VERSION.SDK_INT >= 28){ //onItemSelected() doesn't seem to be called when listener is set on Api 28+
check = 1;
}
if(++check > 1) {
//Do your action here
}
}
I think that's quite weird and I'm not sure wether others also have this problem, but in my case it worked well.

I placed a TextView on top of the Spinner, same size and background as the Spinner, so that I would have more control over what it looked like before the user clicks on it. With the TextView there, I could also use the TextView to flag when the user has started interacting.
My Kotlin code looks something like this:
private var mySpinnerHasBeenTapped = false
private fun initializeMySpinner() {
my_hint_text_view.setOnClickListener {
mySpinnerHasBeenTapped = true //turn flag to true
my_spinner.performClick() //call spinner click
}
//Basic spinner setup stuff
val myList = listOf("Leonardo", "Michelangelo", "Rafael", "Donatello")
val dataAdapter: ArrayAdapter<String> = ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, myList)
my_spinner.adapter = dataAdapter
my_spinner.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
if (mySpinnerHasBeenTapped) { //code below will only run after the user has clicked
my_hint_text_view.visibility = View.GONE //once an item has been selected, hide the textView
//Perform action here
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {
//Do nothing
}
}
}
Layout file looks something like this, with the important part being that the Spinner and TextView share the same width, height, and margins:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/my_spinner"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="#drawable/bg_for_spinners"
android:paddingStart="8dp"
android:paddingEnd="30dp"
android:singleLine="true" />
<TextView
android:id="#+id/my_hint_text_view"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="#drawable/bg_for_spinners"
android:paddingStart="8dp"
android:paddingEnd="30dp"
android:singleLine="true"
android:gravity="center_vertical"
android:text="*Select A Turtle"
android:textColor="#color/green_ooze"
android:textSize="16sp" />
</FrameLayout>
I'm sure the other solutions work where you ignore the first onItemSelected call, but I really don't like the idea of assuming it will always be called.

I solved this problem like this:
In the activity lifecycle method whose name is onResume():
I added Spinner.setOnItemSelectedListener(this);
As a result, when our spinner call onclick method in the initialize, it does not work.
onResume method starts working when the finished Android page is displayed.

Related

Android: Onclick not working in VerticalGridView

I have an activity which creates a fragment with VerticalGridView.
In activity I have this:
#Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if (fragment != null) {
if (fragment.handleKeyDown(keyCode)) {
return true;
}
// todo swallow some events
}
return super.onKeyDown(keyCode, event);
}
Since I need to do some special treatment for UP, DOWN, LEFT and RIGHT so I override this onKeyDown.
Then I have a custom viewHolder GridItemViewHolder extends RecyclerView.ViewHolder, in this viewHolder I have:
gridItemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LOG.d("onclick!!");
}
});
But this onclick method is never called when I press "Enter". Am I missing something here? Is onKeyDown override the onClick?
Thanks!!
Use OnItemClickListener for GridView
gridItemView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int i, long id) {
LOG.d("onclick!!"+i);
}
});

Android - Advanced Usage for Spinner.OnItemSelectedListener

I usually use boolean for this but in this situation I can not. I have a code like below on my Spinner and everything works fine :
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
carNames = brand.getChildCarNames(brand.getListDataHeader().get(position));
makeAndShowDialogBox();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
The problem is when I use bolean what happens is that the choice that comes as Default is not showing the DialogBox if user chooses that Default value as-well. Such as "Tomato" comes as Default and user wants to select "Tomato" as well; nothing happens than.
What I want to do is also prevent this dialog box coming asap when Activity is opened but also I want to prevent "on selection of default value nothing happens" issue.
So, Is there any way to check exactly if the User pressed to select or not?
You can use a Boolean variable to determine if the spinner is just loaded, so you can prevent the dialog to show when the Activity is opened (or onResume). You can declare a variable outside the listener, and change its value inside the listener. Its value is changed inside the listener, so you have to do final Boolean array instead of regular Boolean.
You can try with the following code:
final Boolean[] spinnerLoaded = {false};
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if (!spinnerLoaded[0]) {
spinnerLoaded[0] = true;
}
else {
carNames = brand.getChildCarNames(brand.getListDataHeader().get(position));
makeAndShowDialogBox();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});

Spinner's onItemSelected callback called twice after a rotation if non-zero position is selected

When I create my activity, I setup a Spinner, assigning it a listener and an initial value. I know that the onItemSelected callback is called automatically during application initialization. What I find strange is that this happens twice when the device is rotated, causing me some problems that I will have to circumvent someway. This does not happen if the spinner initial selection is zero. I was able to isolate the issue, here's the simplest activity triggering it:
public class MainActivity extends Activity implements OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Test","Activity onCreate");
setContentView(R.layout.activity_main);
((Spinner)findViewById(R.id.spinner1)).setSelection(2);
((Spinner)findViewById(R.id.spinner1)).setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> spin, View selview, int pos, long selId)
{
Log.i("Test","spin:"+spin+" sel:"+selview+" pos:"+pos+" selId:"+selId);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
}
And here's the logcat shown when the application is started and then the device rotated:
I/Test( 9881): spin:android.widget.Spinner#4052f508 sel:android.widget.TextView#40530b08 pos:2 selId:2
I/Test( 9881): Activity onCreate
I/Test( 9881): spin:android.widget.Spinner#40535d80 sel:android.widget.TextView#40538758 pos:2 selId:2
I/Test( 9881): spin:android.widget.Spinner#40535d80 sel:android.widget.TextView#40538758 pos:2 selId:2
Is this the expected behaviour? Am I missing something?
Managed to find a solution in another stackoverflow question:
spinner.post(new Runnable() {
public void run() {
spinner.setOnItemSelectedListener(listener);
}
});
In general, there seem to be many events that trigger the onItemSelected call, and it is difficult to keep track of all of them. This solution allows you to only respond to user-initiated changes using an OnTouchListener.
Create your listener for the spinner:
public class SpinnerInteractionListener implements AdapterView.OnItemSelectedListener, View.OnTouchListener {
boolean userSelect = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
userSelect = true;
return false;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (userSelect) {
// Your selection handling code here
userSelect = false;
}
}
}
Add the listener to the spinner as both an OnItemSelectedListener and an OnTouchListener:
SpinnerInteractionListener listener = new SpinnerInteractionListener();
mSpinnerView.setOnTouchListener(listener);
mSpinnerView.setOnItemSelectedListener(listener);
The first time the onItemSelected runs, the view is not yet inflated. The second time it is already inflated. The solution is to wrap methods inside onItemSelected with if (view != null).
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (view != null) {
//do things here
}
}
Just use setSelection(#, false) before setting the listener:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
spinner.setSelection(2, false);
spinner.setOnItemSelectedListener(this);
}
The key is the second parameter. It says to not animate the transition, executing the action immediately and preventing onItemSelected from being fired twice, because a call is already made by the system.
This is what i did:
Do a local variable
Boolean changeSpinner = true;
On the saveInstanceMethod save the selected item position of the spinner
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("ItemSelect",mySpinner.getSelectedItemPosition());
}
Then on the activity created get that int from savedInstanceState and if the int is != 0 then set the boolean variable on false;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
if (savedInstanceState!=null) {
if (savedInstanceState.getInt("ItemSelect")!=0) {
changeSpinner = false;
}
}
}
And for last on the OnItemSelected from the spinner do this
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,android.view.View v, int position, long id) {
if (changeSpinner) {
[...]
} else {
changeSpinner= true;
}
});
So, the first time when is called is not going to do anything, just make the boolean variable true, and the second time is going to execute the code.
Maybe not the best solution but it work.
I am updating #Andres Q.'s answer in Kotlin.
Create an inner class in which you're using Spinner
inner class SpinnerInteractionListener : AdapterView.OnItemSelectedListener, View.OnTouchListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
if (userSelect) {
//Your selection handling code here
userSelect = false
}
}
#SuppressLint("ClickableViewAccessibility")
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
userSelect = true
return false
}
internal var userSelect = false
}
Then declare instance variable outside onCreate() as globally like
lateinit var spinnerInteractionListener: SpinnerInteractionListener
then initialise it inside onCreate() by
spinnerInteractionListener = SpinnerInteractionListener()
and use it like
spinnerCategory.onItemSelectedListener = spinnerInteractionListener
spinnerCategory.setOnTouchListener(spinnerInteractionListener)
here spinnerCategory is Spinner
Try this:
boolean mConfigChange = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
mConfigChange = false;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainf);
Log.i("SpinnerTest", "Activity onCreate");
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.colors,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
((Spinner) findViewById(R.id.spin)).setAdapter(adapter);
((Spinner) findViewById(R.id.spin)).setSelection(2);
((Spinner) findViewById(R.id.spin)).setOnItemSelectedListener(this);
}
#Override
protected void onResume() {
mConfigChange = true;
super.onResume();
}
#Override
public void onItemSelected(AdapterView<?> spin, View selview, int pos, long selId) {
if (!mConfigChange)
Log.i("Test", "spin:" + spin + " sel:" + selview + " pos:" + pos + " selId:" + selId);
else
mConfigChange = false;
}
You can just call to setSelection once you know have the list of items and the position to be selected, in that way you avoid onItemSelected to be called twice.
I've created an article about what I think is a better approach How to avoid onItemSelected to be called twice in Spinners
I wrote an extension function that skips all selection events except those initiated by the user. Dont forget to override defPosition if you use not first default spinner position
fun Spinner.setFakeSelectSkipWatcher(execute: (position: Int) -> Unit, defPosition: Int = 0) {
val listener = object : AdapterView.OnItemSelectedListener {
var previousIsNull = -1
var notSkip = false
override fun onItemSelected(p0: AdapterView<*>?, view: View?, position: Int, p3: Long) {
if (notSkip) execute(position)
else {
if ((view != null && position == defPosition) ||
(view == null && position == defPosition) ||
(view != null && previousIsNull == 1 && position != defPosition)
) notSkip = true
}
previousIsNull = if (view == null) 1 else 0
}
override fun onNothingSelected(p0: AdapterView<*>?) {}
}
onItemSelectedListener = listener
}
In kotlin stateflow make it easy. In fragment also it can hold data on rotation. In my code I solved as
In ViewModel:
private val _selectedPosition = MutableStateFlow(0)
val selectedPosition = _selectedPosition.asStateFlow()
fun setPosition(position: Int) {
_selectedPosition.value = position
}
in Fragment
val selectedPosition= viewModel.selectedPosition.value
spinner.setSelection(selectedPosition)

Spinner OnItemSelectedListener Issue

I have problem with spinner control. I am trying to set spinner items dynamically. Initially I have one item in spinner.
When I try to register the spinner.setOnItemSelect Listener, it immediately call onItemSelected method of it. However I don't want to call this method as soon as my activity get started.
So for this I put a following condition.
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
Spinner spinner;
String[] str_arr = {"aaaaaaaa"};
private int mSpinnerCount=0;
private int mSpinnerInitializedCount=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str_arr));
spinner.setOnItemSelectedListener(this);
mSpinnerCount=1;
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (mSpinnerInitializedCount < mSpinnerCount) {
mSpinnerInitializedCount++;
}
else {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
But when I try to select an item on spinner it gives following warning in logcat,
09-03 13:02:02.528: W/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#450fafb8
I get the idea that until and unless Item of spinner won't change this method won't be called.
But I have one value in spinner, so how to get the focus, any idea?
Try this to what i said in comment...
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (position > 0) {
//Your actions
}
else {
// Nothing or can show a toast to say user to select a value...
}
}
Try like this
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (position != 0) {
//put your actions here
}
else {
// nothing here or toast
}
}
I think the below code are not right because you implements OnItemSelectedListener
spinner.setOnItemSelectedListener(this);
You get this warning when you try to open already opened window, or try to do something like onFocus on already focused view.
Here you already have the item selected in the Spinner

Android Spinner - onItemSelected / setOnItemSelectedListener not triggering

This is driving me nuts since it's something I've done before but can't figure out why it isn't working now...
I've got a menu button, implemented in the usual way via a menu.xml file and the onOptionsItemSelected method with a switch in it, that creates and displays a spinner.
I've added the setOnItemSelectedListener, but it never seems to trigger. The spinner appears, I pick an option or back out, neither onItemSelected or onNothingSelected are called.
Here is all the code between the "case" and "return true" of the menu-button-handling switch statement. (topThis is a variable referring to the context of the activity - works fine for all other toasts in the app)
String[] widgetModes = {"Mode 1", "Mode2"};
ArrayAdapter<String> widgetModeAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, widgetModes);
widgetModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner widgetModeSpinner = new Spinner(this);
widgetModeSpinner.setAdapter(widgetModeAdapter);
widgetModeSpinner.setPrompt("Choose Widget Mode");
widgetModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
Toast.makeText(topThis, "derp", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
Toast.makeText(topThis, "herf", Toast.LENGTH_LONG).show();
}
});
widgetModeSpinner.performClick();
Any ideas? I vaguely suspect that the fact I'm creating the Spinner programmatically is the problem...
I had the similar problem when I was implementing a spinner, I resolved it by getting the parent View and set Adapter-
spinner1 =(Spinner)findViewById(R.id.spinner1);
spinner1.setAdapter(BindSpinner("ProgramMaster",cols,null,true,""));
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
protected Adapter initializedAdapter=null;
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
if(initializedAdapter !=parentView.getAdapter() ) {
initializedAdapter = parentView.getAdapter();
return;
}
String selected = parentView.getItemAtPosition(position).toString();
if(abc.equals("Select") && !selected.equals("Select"))
{
do something
}
else
{
Do something
}
textQualification=selected;
SearchUpdated("Qualification");
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Remember that you can't re-select the same spinner item, it always sets the first item as selected if you are not adding some custom code to handle the spinner selection.
For the Toast not showing, I would suggest to always use the "MyActivity.this" as your context when creating a Toast inside a listener interface like this:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
/**
* Called when a new item is selected (in the Spinner)
*/
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An spinnerItem was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
Toast.makeText(MyActivity.this, "Hello Toast",Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing, just another required interface callback
}
}); // (optional)
And the .show() at the end is easy to forget sometimes;)
Actually, if your spinner visibility is set to gone then it will trigger the click of it when you call performclick() method but it will not trigger its setOnItemSelectedListener
so you need to change the visibility then it will work
I know the question is a bit old, but in case you are waiting for a AsyncTask callback, make sure that you let your adapter know of the data changes by calling notifyDataSetChanged() on the callback thread!
#Override
public void onPostExecute(String result) {
///do something with your data
spinnerArrayAdapter.notifyDataSetChanged();
}

Categories

Resources