I have a requirement to show an AlertDialog
when selecting the 2nd item in Spinner
. I know that using onItemSelected
we can listen to the spinner selection & show a popup. The issue is when I select the 2nd item, the dialog appears but after closing the dialog and then again we select the same item, it won't show the dialog as onItemSelected
will not be invoked. Is there any workaround for this? Without using a custom Spinner
implementation.
Android Spinner: onItemSelected listener is not called when selecting the same item
Asked Answered
Create a custom spinner
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
public class CustomSpinner extends Spinner {
OnItemSelectedListener listener;
private AdapterView<?> lastParent;
private View lastView;
private long lastId;
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
initlistner();
}
@Override
public void setSelection(int position) {
if (position == getSelectedItemPosition() && listener != null) {
listener.onItemSelected(lastParent, lastView, position, lastId);
} else {
super.setSelection(position);
}
}
private void initlistner() {
// TODO Auto-generated method stub
super.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
lastParent = parent;
lastView = view;
lastId = id;
if (listener != null) {
listener.onItemSelected(parent, view, position, id);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
if (listener != null) {
listener.onNothingSelected(parent);
}
}
});
}
public void setOnItemSelectedEvenIfUnchangedListener(
OnItemSelectedListener listener) {
this.listener = listener;
}
}
Set Listener
private OnItemSelectedListener listener;
listener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
Pass the listener object to custom listener
cusSpinner.setOnItemSelectedEvenIfUnchangedListener(listener);
Thanks..actually I needed if this can be done without custom spinner..I knew a Custom Spinner can easily handle this.. –
Gleeson
I am trying to add this feature to react-native but am not sure how to modify it to acheive this, may you please give some tips on the minimal changes I need to make to achieve this in the file here - github.com/facebook/react-native/blob/… - I'm a javascript guy, this is my first venture into java. –
Semiporcelain
FYR- ISSUE 15556 –
Eal
How to instantiate cusSpinner ? –
Disturbance
This is working solution, thanks for saving my day –
Graceless
try the code below:
spinner.setOnItemSelectedListener(this);
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
switch(arg2)
{
case 0:
{
Dialog dialog = new Dialog(getApplicationContext());
.......
dialog.show();
spinner.setSelection(0);
}
break;
}
[...]
Please test this code yourself & try whether it works as expected. (This doesn't work) –
Gleeson
© 2022 - 2024 — McMap. All rights reserved.
Spinner
with a listener that checks theposition
arguments and does something is not a hard thing. I think some poeple downvote questions they don't understand, for some reason. – Unpackspinner.setSelection(Spinner.INVALID_POSITION);
in the listener, soonItemSelected
always triggers anonNothingSelected
event too. This means that you'll lose tracking the selected item, but I have a feeling it's not a big deal for this situation. You can also try only calling whenposition == 1
. – Unpack