基于另一个微调选择更改微调 [英] Change Spinner based on another Spinner Selection

查看:179
本文介绍了基于另一个微调选择更改微调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串数组包含所有国家名称和相应国家codeS另一个字符串数组。
我设置的国名字符串的好手。
当我选择从微调的项目分别为本国code应设置为微调。而该项目也应更改为code。
同样,如果我选择另一项目列入code应设置为微调器和项目了。但我选择了第一个应该改变其原来的国名。
我应该怎么做呢?

XML

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=com.example.temp.MainActivity><微调
    机器人:ID =@ + ID / spinnerCountry
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentTop =真
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginTop =124dp/>  < / RelativeLayout的>

Java的code

 包com.example.temp;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.ArrayAdapter;
进口android.widget.Spinner;公共类MainActivity延伸活动{     最终的String []国家=新的String [] {阿富汗(AF),阿尔巴尼亚(AX),
                                              阿尔及利亚(AL),美属萨摩亚(DZ),
                                              安道尔(AS)};    最终的String []国家codeS =新的String [] {AF,AX,AL,DZ,AS};    微调spinnerCountry;    ArrayAdapter<串GT; arrayAdapter;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        spinnerCountry =(微调)findViewById(R.id.spinnerCountry);        arrayAdapter =新ArrayAdapter<串GT;(getApplicationContext()
                                         android.R.layout.simple_list_item_1,国家);
        spinnerCountry.setAdapter(arrayAdapter);
    }
}

因为我在我的应用程序使用它,我不能分享我的原创。
但是,我共享同一个code像我原来的。
我想设置相应的code,当我从微调选择国家。


解决方案

 的String []国家= {印,迪拜,KSA};
        的String [] country_ code = {91,971,966};        最后的微调spinnerCountry =(微调)findViewById(R.id.spinnerCountry);
        最后的微调spinnerCountry code =(微调)findViewById(R.id.spinnerCountry code);        ArrayAdapter<串GT; countryAdapter =新ArrayAdapter<串GT;(getApplicationContext(),android.R.layout.simple_list_item_1,国家);        ArrayAdapter<串GT;国家codeAdapter =新ArrayAdapter<串GT;(getApplicationContext(),android.R.layout.simple_list_item_1,country_ code);        spinnerCountry.setAdapter(countryAdapter);
        spinnerCountry code.setAdapter(国家codeAdapter);        spinnerCountry.setOnItemSelectedListener(新OnItemSelectedListener(){            @覆盖
            公共无效onItemSelected(适配器视图<>为arg0,ARG1查看,INT位置,长ARG3){
                spinnerCountry code.setSelection(位置);
            }            @覆盖
            公共无效onNothingSelected(适配器视图<>为arg0){            }        });        spinnerCountry code.setOnItemSelectedListener(新OnItemSelectedListener(){            @覆盖
            公共无效onItemSelected(适配器视图<>为arg0,ARG1查看,INT位置,长ARG3){                spinnerCountry.setSelection(位置);
            }            @覆盖
            公共无效onNothingSelected(适配器视图<>为arg0){            }        });

下面是一些code,可能会解决您的问题。

说明:

假设你有两个微调R.id.spinnerCountry和R.id.spinnerCountry code。

正如你所看到的,我创建了两个数组,分别包含国家名称和国家code。

工作流程: -

当我在微调中选择一个项目,然后其他的微调负载与对应位置的另一个项目。

在code是不言自明!

I have string array which contains all the country names and another string array with respective country codes. I am setting the country names string to spinner. When I select the item from spinner its respective country code should be set to the spinner. And that item also should change to Code. Similarly if I select another item its code should be set to spinner and item too. But the first one I selected should change to its original country name. How should I do this?

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.temp.MainActivity" >

<Spinner
    android:id="@+id/spinnerCountry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="124dp" />

  </RelativeLayout>

Java Code

package com.example.temp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

     final String[] countries = new String[] {"Afghanistan(AF)", "Albania(AX)",
                                              "Algeria(AL)", "American    Samoa(DZ)",
                                              "Andorra(AS)"};

    final String[] countrycodes = new String[]{"AF","AX","AL","DZ","AS"};

    Spinner spinnerCountry;

    ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinnerCountry=(Spinner)findViewById(R.id.spinnerCountry);

        arrayAdapter =  new ArrayAdapter<String>(getApplicationContext(), 
                                         android.R.layout.simple_list_item_1,countries);
        spinnerCountry.setAdapter(arrayAdapter);
    }
}

I can't share my original as I am using it in my application. But, I am sharing the same code like my original one. I want to set respective code when I select country from spinner.

解决方案

String[] countries = {"INDIA","DUBAI","KSA"};
        String[] country_code = {"91","971","966"};

        final Spinner spinnerCountry = (Spinner)findViewById (R.id.spinnerCountry);
        final Spinner spinnerCountryCode = (Spinner) findViewById (R.id.spinnerCountryCode);

        ArrayAdapter<String> countryAdapter =  new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,countries);

        ArrayAdapter<String> countryCodeAdapter =  new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,country_code);

        spinnerCountry.setAdapter(countryAdapter);
        spinnerCountryCode.setAdapter(countryCodeAdapter);

        spinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
                spinnerCountryCode.setSelection(position);
            }

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

            }

        });

        spinnerCountryCode.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {

                spinnerCountry.setSelection(position);
            }

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

            }

        });

Here is some code that may fix your problem.

Explanation:

Assuming you have two spinners R.id.spinnerCountry and R.id.spinnerCountryCode.

As you can see i created two arrays which contains countryName and countryCode respectively.

Work Flow:-

When i select one item in a Spinner then the other spinner loads with another item corresponding to the position.

The code is Self-Explanatory!

这篇关于基于另一个微调选择更改微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆