android – 一个活动中的两个微调器

前端之家收集整理的这篇文章主要介绍了android – 一个活动中的两个微调器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有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,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

亚历克斯

解决方法

使用
spin2.setAdapter(bb);

代替

spin.setAdapter(bb);

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

猜你在找的Android相关文章