nice-spinner
nice-spinner copied to clipboard
setOnItemSelectedListener
mSpinner.setOnItemSelectedListener(new SimpleItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { playVideo(); } });
Hello, i find a bug,this Listener,default 0 item was selected?? , but playVideo() not run.why?
Is not a bug , unfurtunatelly is the base android behaviour
Use AdapterView.OnItemSelectedListener instead of SimpleItemSelectedListener
Spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("Spinner",list.get(position));
//Do your thing here
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Log.d("Spinner","Nothing Selected!");
}
});
I have same problem but I already have used AdapterView & set selectedIndex to 0. Still not working. Tried Code:
spinnerProperties.attachDataSource(propertyNamesList);
spinnerProperties.setSelectedIndex(0);
spinnerProperties.setSelected(true);
spinnerProperties.setOnItemSelectedListener(onPropertyNameSelected);
AdapterView.OnItemSelectedListener onPropertyNameSelected = new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
Log.d(TAG, "Selected Property: " + position);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
};
please help me to solve this issue @arcadefire
Not sure what's the problem here.
The problem is, that when we set selected index programmatically, OnItemSelectedListener is not called. @arcadefire is it a normal behavior? How can I run listener when changed selected index in code?
@arcadefire could you just let us know if this is normal behavior and we need to handle it by ourselves or just a bug?
@pzienowicz sorry, I missed the previous message. I think the listener should be called when selecting the item programmatically. I'm adding this to my TODO list, or feel free to open a PR. :)
termsSpinner.setSelectedIndex(1); // set selection to any non required index
termsSpinner.getListView().performItemClick(termsSpinner.getListView().getChildAt(0), 0, termsSpinner.getListView().getAdapter().getItemId(0)); // 0 is the position we require to be clicked
selectDivision.setText((selectDivisionSpinner.selectedItem as State).name)
We can get selected item from spinner and cast it accordingly, after that we can use its properties
To programatically select an option and call its click listener, I added this library as module to my project so that I can edit its file.
selectSiteSpinner.selectedIndex =0
val fakeView = this.context?.let { FrameLayout(it) }
selectSiteSpinner.onSpinnerItemSelectedListener.onItemSelected(
selectSiteSpinner,
selectSiteSpinner.adapter.getView(selectSiteSpinner.selectedIndex, null, fakeView),
selectSiteSpinner.selectedIndex,
selectSiteSpinner.adapter.getItemId(selectSiteSpinner.selectedIndex)
)