10

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

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

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

BR

亚历克斯

4

5 回答 5

5

不要将实现添加到您的 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();        
        }
    }
}
于 2012-12-17T06:21:34.007 回答
4

利用

spin2.setAdapter(bb);

代替

spin.setAdapter(bb);

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

于 2012-12-17T06:08:05.040 回答
1
bb.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(bb); (should be second spinner..)
^^^^^
spin2.setOnItemSelectedListener(this);
于 2012-12-17T06:11:25.947 回答
0

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

于 2012-12-17T06:11:23.787 回答
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 类

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

更多信息 的资源链接图书馆 SmartMaterialSpinner

于 2019-08-26T15:26:52.407 回答