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

このアクティビティを実行すると、最初のスピナー値はcityで、2番目のスピナー値は空です。

同じアクティビティで2つのスピナーを実行するにはどうすればよいですか?最初のスピナー値は国で、2番目のスピナー値は都市です。

BR

アレックス

4

5 に答える 5

5

Activityクラスに実装を追加しないでください

それぞれがスピナーの1つを処理する2つの内部クラスを作成します

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

現在、2番目のスピナーにアダプターを設定しています

于 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 に答える