nice-spinner icon indicating copy to clipboard operation
nice-spinner copied to clipboard

setOnItemSelectedListener

Open SkyHots opened this issue 6 years ago • 10 comments

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?

SkyHots avatar Jan 31 '18 02:01 SkyHots

Is not a bug , unfurtunatelly is the base android behaviour

gisk4rd avatar Oct 12 '18 14:10 gisk4rd

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!");
                }
            });

arunavo4 avatar Dec 16 '18 20:12 arunavo4

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

ideep avatar Feb 12 '19 05:02 ideep

Not sure what's the problem here.

arcadefire avatar May 24 '19 08:05 arcadefire

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?

pzienowicz avatar Jun 08 '19 17:06 pzienowicz

@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 avatar Jun 24 '19 17:06 pzienowicz

@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. :)

arcadefire avatar Jun 24 '19 18:06 arcadefire

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

ashish-dhiman avatar Oct 09 '19 08:10 ashish-dhiman

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

ashish-d-hh avatar Aug 20 '20 09:08 ashish-d-hh

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)
    )

ashish-d-hh avatar Sep 16 '20 10:09 ashish-d-hh