Android Spinner: Get the selected item change event

前端 未结 16 2209
挽巷
挽巷 2020-11-22 16:47

How can you set the event listener for a Spinner when the selected item changes?

Basically what I am trying to do is something similar to this:

spinn         


        
相关标签:
16条回答
  • 2020-11-22 17:29
    spinner.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {
    
                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
    
                        // TODO Auto-generated method stub
                    }
    
                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub
    
                    }
                    //add some code here
                }
            );
    
    0 讨论(0)
  • 2020-11-22 17:29

    The best way what I think would be to have an flagitemselected = 0; in onCreate(). And on item selected event increment that flag i.e flagitemselected++; and then check

    if(flagitemselected!=1)
    {
    // do your work here
    }
    

    This will help I guess.

    0 讨论(0)
  • 2020-11-22 17:31

    take a global variable for current selection of spinner:

    int currentItem = 0;
    
    spinner_counter = (Spinner)findViewById(R.id.spinner_counter);
    String[] value={"20","40","60","80","100","All"};
    aa=new ArrayAdapter<String>(this,R.layout.spinner_item_profile,value);
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_counter.setAdapter(aa);
    
    spinner_counter.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(currentItem == position){
                    return; //do nothing
                }
                else
                {
                     TextView spinner_item_text = (TextView) view;
                     //write your code here
                }
                currentItem = position;
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
    
            }
        });
    
    //R.layout.spinner_item_profile
    <?xml version="1.0" encoding="utf-8"?>
    
    <TextView  android:id="@+id/spinner_item_text"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:background="@drawable/border_close_profile"
    android:gravity="start"  
    android:textColor="@color/black"         
    android:paddingLeft="5dip"
    android:paddingStart="5dip"
    android:paddingTop="12dip"
    android:paddingBottom="12dip"
    />
    
    //drawable/border_close_profile
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item>
       <shape android:shape="rectangle">
        <solid android:color="#e2e3d7" />
       </shape>
     </item>
    <item android:left="1dp"
    android:right="1dp"
    android:top="1dp"
    android:bottom="1dp">
    <shape android:shape="rectangle">
        <solid android:color="@color/white_text" />
    </shape>
    </item>
    </layer-list>
    
    0 讨论(0)
  • 2020-11-22 17:33
    spinner1.setOnItemSelectedListener(
        new AdapterView.OnItemSelectedListener() {
            //add some code here
        }
    );
    
    0 讨论(0)
  • 2020-11-22 17:34

    You can implement AdapterView.OnItemSelectedListener class in your Activity.

    And then use the below line within onCreate()

    Spinner spin = (Spinner) findViewById(R.id.spinner);
    spin.setOnItemSelectedListener(this);
    

    Then override these two methods:

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        selection.setText(items[position]);
    }
    
    public void onNothingSelected(AdapterView<?> parent) {
        selection.setText("");
    }
    
    0 讨论(0)
  • 2020-11-22 17:34

    For kotlin you can use:

    spinner.onItemSelectedListener =  object : AdapterView.OnItemSelectedListener {
        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            
        }
    
        override fun onNothingSelected(p0: AdapterView<*>?) {
            
        }
    }
    

    Note: for parameters of onItemSelected method I use custom variable names

    0 讨论(0)
提交回复
热议问题