两个旋转器在一个活动中

时间:2012-12-17 06:05:57

标签: android android-spinner

我有2个旋转器。

这是我的活动

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();
    }
}

当我运行此活动时,第一个微调器值为city,第二个微调器值为空。

如何在同一活动中运行2个微调器? 第一个微调器值是国家,第二个微调器值是城市。

BR

亚历

5 个答案:

答案 0 :(得分:4)

使用

spin2.setAdapter(bb);

而不是

spin.setAdapter(bb);

目前您正在为第二个微调器设置适配器

答案 1 :(得分:1)

bb.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(bb); (should be second spinner..)
^^^^^
spin2.setOnItemSelectedListener(this);

答案 2 :(得分:1)

不要将实现添加到Activity类

创建两个内部类,每个内部类处理一个微调器

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();        
        }
    }
}

答案 3 :(得分:0)

你应该使用make spin.setAdapter(bb); to spin2.setAdapter(bb);

答案 4 :(得分:0)

我在微调器方面遇到了很多问题,终于找到了解决方案

此库( SmartMaterialSpinner )为您节省了很多时间,因为它提供了一种轻松添加所需数量的微调器的简便方法,并且可以轻松地独立添加项目到微调器中侦听器

这是我在项目中的使用方式:

 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) {

        }
    });

WorkAround类将字符串数组转换为字符串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;
        }
    }
}

请注意,您无需实现

implements AdapterView.OnItemSelectedListener

资源链接以获取更多信息  The Library SmartMaterialSpinner