Android Spinner: Get the selected item change event

前端 未结 16 2238
挽巷
挽巷 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: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(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
    
    
    
    
    //drawable/border_close_profile
    
    
      
       
        
       
     
    
    
        
    
    
    
    

提交回复
热议问题