根据另一个微调器选择更改微调器

时间:2015-02-24 17:19:06

标签: android android-spinner

我有字符串数组,其中包含所有国家/地区名称和另一个包含相应国家/地区代码的字符串 我将国家/地区名称字符串设置为微调器。 当我从微调器中选择项目时,其相应的国家/地区代码应设置为微调器。该项目也应改为Code。 同样,如果我选择另一个项目,其代码也应设置为微调器和项目。但我选择的第一个应更改为其原始国家/地区名称。 我该怎么做?

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代码

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

我无法分享我的原件,因为我在我的应用程序中使用它。 但是,我和我原来的代码共享相同的代码。 当我从微调器中选择国家时,我想设置相应的代码。

2 个答案:

答案 0 :(得分:2)

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

            }

        });

以下是一些可以解决问题的代码。

说明:

假设您有两个微调器R.id.spinnerCountry和R.id.spinnerCountryCode。

如您所见,我创建了两个分别包含countryName和countryCode的数组。

工作流程: -

当我在Spinner中选择一个项目时,另一个微调器会加载另一个与该位置对应的项目。

代码是自我解释的!

答案 1 :(得分:0)

String businesstype_array [] = {“Retail”,“Vendor”,“Agriculture”};

public static final String JSON_STRING = "{\"Retail\":[\"General Stores\",\"Clothing\",\"Electronics\",\"Mobile Phones\"],\"Vendor\":[\"Gas agency\",\"newspaper\",\"vendor\",\"milk vendor\",\"laundry\"],\"Agriculture\":[\"lGas agency\",\"lnewspaper\",\"lvendor\",\"lmilk vendor\",\"llaundry\"]}";

ArrayAdapter adapter_business = new ArrayAdapter(getActivity(),R.layout.singlerow_businesstype_spinner,businesstype_array);         businesstype.setAdapter(adapter_business);

    businesstype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            Log.d("jsonntring", "out tryy");
            try {
                Log.d("jsonntring", "in tryy");


                jsonObject = new JSONObject(JSON_STRING);
                Log.d("jsonntring", jsonObject.toString() + "length==" + jsonObject.length());


                JSONArray jsonarray = jsonObject.getJSONArray(businesstype.getSelectedItem().toString());

                Log.d("jsonarray&", jsonarray.toString());

                businesstype__subtype.clear();
                for (int j = 0; j < jsonarray.length(); j++) {
                    String s = jsonarray.getString(j);

                    businesstype__subtype.add(s);

                    Log.d("jsonarray&", jsonarray.toString());

                }

                ArrayAdapter adapter_sub_business = new ArrayAdapter(getActivity(), R.layout.singlerow_businesstype_spinner, businesstype__subtype);
                business_subtype.setAdapter(adapter_sub_business);

            } catch (JSONException e)
            {
                Log.d("jsonntring_exce", e.toString());

            }


        }

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

        }
    });