two spinner in one activity
Asked Answered
P

5

10

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

Pizzeria answered 17/12, 2012 at 6:5 Comment(0)
B
4

use

spin2.setAdapter(bb);

instead of

spin.setAdapter(bb);

currently you are setting Adapter for second spinner

Belligerence answered 17/12, 2012 at 6:8 Comment(17)
i have one question again. i have a form input page. example SaveActivity.java. in this activity, i have a EditText, Button. how to import this CreateComboBox to SaveActivity.java. so, i have spinner in SaveActivity.java? @ρяσѕρєя KPizzeria
@Alexbelek : u have two spinner in CreateComboBox and you want to move selected value to SaveActivity from CreateComboBox right?Patrickpatrilateral
i Edited My Question . i add a capture SaveActivity.java when running in my question. but the spinner is empty value. how to import value Countries and City inside this page? thanks @ρяσѕρєя KPizzeria
@Alexbelek : but you have both spinner in CreateComboBox Activity?Patrickpatrilateral
@Alexbelek : you want user selected data from spinner of CreateComboBox Activity to saveActivity? plz make me clearPatrickpatrilateral
yes @ρяσѕρєя K. i have a save.xml, CreateCombobox and saveActivity. so the user can selected data in saveActivity, and spinner of Createcombobox. so the result is the second capture i Edited now. sory im newbie in android, and bad english.Pizzeria
let us continue this discussion in chatPatrickpatrilateral
can you help me for my new question? @ρяσѕρєя K . thanks [#13929112Pizzeria
@Alexbelek : sure where it's give me linkPatrickpatrilateral
[#13929112 thanks @ρяσѕρєя KPizzeria
how? i still problem in spinner and now load to mysql @ρяσѕρєя KPizzeria
@Alexbelek : i saw your question and current issue plz give me some time at evening i will solve your issue and post it as answer.Patrickpatrilateral
yes. ok. i'll wait and keep try. thanks for your help. @ρяσѕρєя KPizzeria
i try the tutorial [a2zandroidtutorials.blogspot.com/2012/07/… but no use sqlite. @ρяσѕρєя KPizzeria
@Alexbelek : chat.stackoverflow.com/rooms/21250/…Patrickpatrilateral
can you help me for my new question? @ρяσѕρєя K . thanks [#14329682Pizzeria
How can I populate cities dynamically from database through API call?Thiele
A
6

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();        
        }
    }
}
Acceptable answered 17/12, 2012 at 6:21 Comment(1)
this answer helped me. i have two different spinners with two different values. i would show only one value for both spinners. but now solved. thakyou.Gabi
B
4

use

spin2.setAdapter(bb);

instead of

spin.setAdapter(bb);

currently you are setting Adapter for second spinner

Belligerence answered 17/12, 2012 at 6:8 Comment(17)
i have one question again. i have a form input page. example SaveActivity.java. in this activity, i have a EditText, Button. how to import this CreateComboBox to SaveActivity.java. so, i have spinner in SaveActivity.java? @ρяσѕρєя KPizzeria
@Alexbelek : u have two spinner in CreateComboBox and you want to move selected value to SaveActivity from CreateComboBox right?Patrickpatrilateral
i Edited My Question . i add a capture SaveActivity.java when running in my question. but the spinner is empty value. how to import value Countries and City inside this page? thanks @ρяσѕρєя KPizzeria
@Alexbelek : but you have both spinner in CreateComboBox Activity?Patrickpatrilateral
@Alexbelek : you want user selected data from spinner of CreateComboBox Activity to saveActivity? plz make me clearPatrickpatrilateral
yes @ρяσѕρєя K. i have a save.xml, CreateCombobox and saveActivity. so the user can selected data in saveActivity, and spinner of Createcombobox. so the result is the second capture i Edited now. sory im newbie in android, and bad english.Pizzeria
let us continue this discussion in chatPatrickpatrilateral
can you help me for my new question? @ρяσѕρєя K . thanks [#13929112Pizzeria
@Alexbelek : sure where it's give me linkPatrickpatrilateral
[#13929112 thanks @ρяσѕρєя KPizzeria
how? i still problem in spinner and now load to mysql @ρяσѕρєя KPizzeria
@Alexbelek : i saw your question and current issue plz give me some time at evening i will solve your issue and post it as answer.Patrickpatrilateral
yes. ok. i'll wait and keep try. thanks for your help. @ρяσѕρєя KPizzeria
i try the tutorial [a2zandroidtutorials.blogspot.com/2012/07/… but no use sqlite. @ρяσѕρєя KPizzeria
@Alexbelek : chat.stackoverflow.com/rooms/21250/…Patrickpatrilateral
can you help me for my new question? @ρяσѕρєя K . thanks [#14329682Pizzeria
How can I populate cities dynamically from database through API call?Thiele
V
1
bb.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(bb); (should be second spinner..)
^^^^^
spin2.setOnItemSelectedListener(this);
Vanesavanessa answered 17/12, 2012 at 6:11 Comment(0)
H
0

You should use make spin.setAdapter(bb); to spin2.setAdapter(bb);

Hedden answered 17/12, 2012 at 6:11 Comment(0)
L
0

I was facing many problems with spinner and finally found this solution

This library (SmartMaterialSpinner) saves you a lot of time as it provides an easy way to add as many spinner as you want, in addition to easy ways to add items to your spinners with stand alone listener

Here is how I used it in my project :

 String[] Durationarray = {"1 Month ", "2 Month ", "3 Month ", "4 Month ", "5 Month", 
         "6 Month ", "7 Month ", "8 Month ", "9 Month ", "10 Month ", "11 Month ",
            "1 Year", "2 Year", "3 Year", "4 Year", "5 Year", "6 Year", "7 Year", "8 Year", "9 Year", "10 Year"};

    spinner1 = (SmartMaterialSpinner) findViewById(R.id.spinner1_id);
    List<String> DurationList = new ArrayList();
    new WorkAround().convertStringArrToListArr(gam3iaDurationarray, DurationList);
    spinner1.setItem(DurationList);
    spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

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

        }
    });

    String[] egyptGovernemt = {"New Valley", "Matruh", "Red Sea", "Giza", "South Sinai", "North Sinai", "Suez", "Beheira", "Helwan", "Sharqia", "Dakahlia",
            "Kafr el-Sheikh" , "Alexandria", "Monufia", "Minya", "Gharbia", "Faiyum", "Qena", "Asyut", "Sohag", "Ismailia", "Beni Suef", "Qalyubia", "Aswan", "Damietta", "Cairo",
            "Port Said", "Luxor", "6th of October"};

    spinner2= (SmartMaterialSpinner) findViewById(R.id.spinner2);
    List<String>  cityList = new ArrayList<String>();
    new WorkAround().convertStringArrToListArr(egyptGovernemt, cityList);

    spinner2.setItem(cityList);
    spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


        }

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

        }
    });

The WorkAround class to convert the string array to string arraylist

public class WorkAround {


    public void convertStringArrToListArr(String [] myStringArray, List<String> myStringList){
        for (String s:myStringArray) {
            myStringList.add( s );
        }
    }

    // Number Checker Method
    public  boolean isNumeric(String str) {
        try {
            Double.parseDouble(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}

Note that you don't need to implements

implements AdapterView.OnItemSelectedListener

The Resources Links for more info The Library SmartMaterialSpinner

Lithea answered 26/8, 2019 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.