简体   繁体   中英

Android Spinner get selected item

I have created 2 spinner in an same activity,

for example

  1. Country
  2. State

Whenever, if Country is selected it will display some countries like Japan,China, etc...

If i select China in another spinner like state which diaplays only china states and city or if i select japan in another spinner it display only japan states

在此处输入图片说明

You can get currently selected item by

String currentSelectedItem = mySpinner.getSelectedItem().toString();

OR

mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Object item = parent.getItemAtPosition(position);
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

I have done same thing in my project :

If you use are getting Country array from JSON than Just follow the step as bellow :

Note: Make sure you also get country_id and state_id to pass country id to state request parameter for same what you want

step 1: First you should fill all countries in to array and set country adapter

step 2: than spinnerCountry.setAdapter() on onCreate() hop you understood

step 3: after than spinnerCountry.setOnItemSelectedListener() you are getting the country_id using position of selected spinner item, pass that country_id to state request parameter for getting state response

step 4: Thats all you get state response and set adater spinnerState.setAdapter() with your response state array

I've finaly added the code for muliple spinner in same activity.

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

Spinner Cou, Sta, Cit;
Button button;
String name[]={"India","SriLanka"};
String flag1="India";
String flag2="SriLanka";
String flag3="Western Province";
String flag4="Central Province";
String flag5="Southern Province";
String name1[]={"Tamilnadu","kerala","karnataka"};
String name2[]={"Western","Central","Southern"};
String name3[]={"Colombo","Gampaha","Kalutara"};
String name4[]={"Kandy","Nuwara-Eliya","Matale"};
String name5[]={"Galle","Matara","Hambantota"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Cou = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name);
    Cou.setAdapter( arrayAdapter);
    Cou.setOnItemSelectedListener(this);

    Sta = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter aadapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name1);
    Sta.setAdapter(aadapter);
    Sta.setOnItemSelectedListener(this);

    Cit = (Spinner) findViewById(R.id.spinner3);
    ArrayAdapter barrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name2);
    Cit.setAdapter(barrayAdapter);
    Cit.setOnItemSelectedListener(this);

    Cit = (Spinner) findViewById(R.id.spinner3);
    ArrayAdapter carrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name3);
    Cit.setAdapter(carrayAdapter);
    Cit.setOnItemSelectedListener(this);

    Cit = (Spinner) findViewById(R.id.spinner3);
    ArrayAdapter darrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name4);
    Cit.setAdapter(darrayAdapter);
    Cit.setOnItemSelectedListener(this);

    Cit = (Spinner) findViewById(R.id.spinner3);
    ArrayAdapter earrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name5);
    Cit.setAdapter(earrayAdapter);
    Cit.setOnItemSelectedListener(this);

    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Main2Activity.class);
            intent.putExtra("data", String.valueOf(Cou.getSelectedItem()));
            intent.putExtra("datam", String.valueOf(Sta.getSelectedItem()));
            intent.putExtra("datama", String.valueOf(Cit.getSelectedItem()));
            startActivity(intent);
        }
    });
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    Object item = parent.getItemAtPosition(position);
    /*Toast.makeText(getApplicationContext(), "Welcome" + item, Toast.LENGTH_SHORT).show();*/

    if(item=="India"){
        Sta = (Spinner) findViewById(R.id.spinner2);
        ArrayAdapter aadapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name1);
        Sta.setAdapter(aadapter);
        Sta.setOnItemSelectedListener(this);

    } else if(item=="SriLanka"){
        Sta = (Spinner) findViewById(R.id.spinner2);
        ArrayAdapter aadapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name2);
        Sta.setAdapter(aadapter);
        Sta.setOnItemSelectedListener(this);

    } else if(item=="Western"){
        Cit = (Spinner) findViewById(R.id.spinner3);
        ArrayAdapter carrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name3);
        Cit.setAdapter(carrayAdapter);
        Cit.setOnItemSelectedListener(this);
    } else if(item=="Central"){
        Cit = (Spinner) findViewById(R.id.spinner3);
        ArrayAdapter darrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name4);
        Cit.setAdapter(darrayAdapter);
        Cit.setOnItemSelectedListener(this);
    }else if(item=="Southern"){
        Cit = (Spinner) findViewById(R.id.spinner3);
        ArrayAdapter earrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,name5);
        Cit.setAdapter(earrayAdapter);
        Cit.setOnItemSelectedListener(this);
    }
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM