Android- Multiple Spinners

后端 未结 2 1731
無奈伤痛
無奈伤痛 2020-12-31 09:36

I have this code:

package lijap.app.starcraft2counters;

import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Enviro         


        
相关标签:
2条回答
  • 2020-12-31 09:49

    You could share the adapter between different Spinners if they adapted the same information. Clearly each of your adapters need to adapt a different set of Strings, so you need to create an ArrayAdapter for each Spinner.

    A single OnItemSelectedListener will work for the 3 Spinners (as long as you set them). You can call getId() on the AdapterView<?> passed as an argument to know which Spinner raised the event.

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        switch(arg0.getId()){
            case R.id.s_answertime:
                break;
            case ...
        }
    }
    
    0 讨论(0)
  • 2020-12-31 09:58

    Check this one

    yearDropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
    
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
        {
            selectedyear = parentView.getSelectedItemPosition();
            drpyear      = yearDropdown.getSelectedItem().toString();
    
            //your code here
    
        }
    
        public void onNothingSelected(AdapterView<?> parentView) 
        {
            selectedyear = 0;
            //return;
        }
    });
    
    monthDropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
    
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
        {
            //your code here
    
    
        }
    
        public void onNothingSelected(AdapterView<?> parentView) 
        {
            selectedmonth = 0;
            //return;
        }
    });
    
    dayDropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
    
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
        {
            //your code here
    
        }
    
        public void onNothingSelected(AdapterView<?> parentView) 
        {
            selectedday = 0;
        }
    });
    

    you need to create each adapter for every spinner.

       adapterYear = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item);
       yearDropdown.setAdapter(adapterYear);
    
       adapterMonth = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item);
       monthDropdown.setAdapter(adapterMonth);
    
       adapterDays = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item);
       dayDropdown.setAdapter(adapterDays);
    
    0 讨论(0)
提交回复
热议问题