Spinner : onItemSelected not called when selected item remains the same
Ok, I finally found a solution, by creating my own class extending Spinner :
public class MySpinner extends Spinner {
OnItemSelectedListener listener;
public MySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setSelection(int position) {
super.setSelection(position);
if (listener != null)
listener.onItemSelected(null, null, position, 0);
}
public void setOnItemSelectedEvenIfUnchangedListener(
OnItemSelectedListener listener) {
this.listener = listener;
}
}
Android spinner, OnItemSelectedListener is not called when selecting the same item?
ok
you can use addOnLayoutChangeListener
yourspinner.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
Toast.makeText(context, "item is : " + yourspinner.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();
}
});
Android Spinner OnItemSelected not called with the same item
I've solved using this class:
public class NDSpinner extends Spinner {
public NDSpinner(Context context)
{ super(context); }
public NDSpinner(Context context, AttributeSet attrs)
{ super(context, attrs); }
public NDSpinner(Context context, AttributeSet attrs, int defStyle)
{ super(context, attrs, defStyle); }
@Override public void
setSelection(int position, boolean animate)
{
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position, animate);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
@Override public void
setSelection(int position)
{
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
}
Thanks anyway :)
Android Spinner: onItemSelected listener is not called when selecting the same item
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);
Android Spinner setOnItemSelectedListener
Create a Custom Class that extends Spinner
and replace it with your Spinner
public class NDSpinner extends Spinner {
public NDSpinner(Context context) {
super(context);
}
public NDSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NDSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void
setSelection(int position, boolean animate) {
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position, animate);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
@Override
public void
setSelection(int position) {
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
}
Why does my spinner not displaying selected item
Edit : Its because jsonGetCompany()
and spinner()
is called same time but volley works asynchronously. You should set adapter after volley gets data from server. Calling this server call from activity is not proper way but it's completely different subject. You can simply edit your code like :
try {
val jsonArray = response.getJSONArray("Table")
for (i in 0 until jsonArray.length()) {
//Adding variables to JSONArray
val item = jsonArray.getJSONObject(i)
companyList.add(item.getString("Company"))
}
setSpinner()
} catch (e: JSONException) {
e.printStackTrace()
}
Glad i could help.
Related Topics
Android:How to Update the Selector(Statelistdrawable) Programmatically
How to Send Notification to Specific Users with Fcm
Why Is the Android Test Runner Reporting "Empty Test Suite"
How to Put an Image in an Alertdialog? Android
Could Not Find Com.Android.Tools.Build:Gradle:4.0.1 ( or Any Version )
Java.Lang.Noclassdeffounderror: Com.Google.Android.Gms.R$Styleable in Android
Occured an Internal_Error When Requestemail from Googlesigninoptions Android
Differencebetween an Intentservice and a Service
Accessing Files from Genymotion Sd Card
How to Tell Which App Was Selected by Intent.Createchooser
App Completely Restarting When Launched by Icon Press in Launcher
Error Type 3: Activity Class {...} Does Not Exist
Limit Scrolling and Zooming Google Maps Android API V2
Toast Created in an Intentservice Never Goes Away
Error String Types Not Allowed at Android:Configchanges in Manifest File