Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two spinner in one activity

i have 2 spinner .

this is my activity

package com.example.belajarcombobox;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class CreateComboBox extends Activity implements AdapterView.OnItemSelectedListener {

    TextView selection;
    TextView selection2;
    String[] countries = {"Amerika","Argentina","Brazil"};

    String[] city = {"London","Paris","Buenos Aires"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        selection = (TextView) findViewById(R.id.selection);
        Spinner spin = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, countries);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(aa);
        spin.setOnItemSelectedListener(this);

        selection2 = (TextView) findViewById(R.id.selection2);
        Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
        ArrayAdapter<String> bb = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, city);
        bb.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(bb);
        spin2.setOnItemSelectedListener(this);
    }

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id ){
        Spinner spin = (Spinner)parent;
        Spinner spin2 = (Spinner)parent;
        if(spin.getId() == R.id.spinner)
        {
            Toast.makeText(this, "Your choose :" + countries[position],Toast.LENGTH_SHORT).show();                 
        }
        if(spin2.getId() == R.id.spinner2)
        {
            Toast.makeText(this, "Your choose :" + city[position],Toast.LENGTH_SHORT).show();       
        }

    }

    public void onNothingSelected(AdapterView<?> parent){
        Toast.makeText(this, "Choose Countries :", Toast.LENGTH_SHORT).show();
    }
}

when i run this activity, the first spinner value is city and the second spinner value is empty.

How do I run 2 spinner in the same activity? First spinner value is countries and second spinner value is city.

BR

Alex

like image 952
Puja Surya Avatar asked Dec 17 '12 06:12

Puja Surya


3 Answers

Don't add the implementation to your Activity class

Create two inner classes that each handle one of the spinners

public class CreateComboBox extends Activity implements AdapterView.OnItemSelectedListener
{
    TextView selection;
    TextView selection2;
    String[] countries = {"Amerika","Argentina","Brazil"};

    String[] city = {"London","Paris","Buenos Aires"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        selection = (TextView) findViewById(R.id.selection);
        Spinner spin = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, countries);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(aa);
        spin.setOnItemSelectedListener(new CountriesSpinnerClass());

        selection2 = (TextView) findViewById(R.id.selection2);
        Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
        ArrayAdapter<String> bb = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, city);
        bb.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(bb);
        spin2.setOnItemSelectedListener(new CitiesSpinnerClass());
    }

    class CountriesSpinnerClass implements AdapterView.OnItemSelectedListener
    {
        void onItemSelected(AdapterView<?> parent, View v, int position, long id)
        {
            Toast.makeText(v.getContext(), "Your choose :" + countries[position],Toast.LENGTH_SHORT).show();      
        }
    }

    class CitiesSpinnerClass implements AdapterView.OnItemSelectedListener
    {
        void onItemSelected(AdapterView<?> parent, View v, int position, long id)
        {
            Toast.makeText(v.getContext(), "Your choose :" + city[position],Toast.LENGTH_SHORT).show();        
        }
    }
}
like image 127
mrres1 Avatar answered Oct 06 '22 01:10

mrres1


use

spin2.setAdapter(bb);

instead of

spin.setAdapter(bb);

currently you are setting Adapter for second spinner

like image 21
ρяσѕρєя K Avatar answered Oct 06 '22 00:10

ρяσѕρєя K


bb.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(bb); (should be second spinner..)
^^^^^
spin2.setOnItemSelectedListener(this);
like image 32
MAC Avatar answered Oct 06 '22 00:10

MAC