android – (动态)多个微调器状态/城市

前端之家收集整理的这篇文章主要介绍了android – (动态)多个微调器状态/城市前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我必须开发一个 android视图,这样我就有2个微调控件,一个用于州,第二个用于城市.

我的问题是,无论何时选择州,我如何自动填充城市微调器?

它背后的逻辑是什么?

我的string.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="spinner_category"></string>
<string-array name="category_state">
    <item >kerala</item>
    <item >tamil nadu</item>
    <item >Andra Pradesh</item>
    <item >karnataka</item>
</string-array>
</resources>

我的main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:text="Select : "/>
        <Spinner android:layout_width="250dip"
            android:layout_height="wrap_content"
            android:id="@+id/spinner_state"/>
    </LinearLayout>

<LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:text="Select : "/>
        <Spinner android:layout_width="250dip"
            android:layout_height="wrap_content"
            android:id="@+id/spinner_state"/>
    </LinearLayout>
</LinearLayout>

还有我的activity.java文件

package com.converter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class ConverterActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner spinner_s = (Spinner)findViewById(R.id.spinner_state);
        ArrayAdapter<CharSequence> category_adapter = ArrayAdapter.createFromResource(
                this,R.array.category_array,android.R.layout.simple_spinner_item);
        category_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_s.setAdapter(category_adapter);

    }
}

解决方法

例:
Spinner city=(Spinner)findViewById(R.id.citySpinner);
    Spinner state=(Spinner)findViewById(R.id.stateSpinner);

    final ArrayList<String> state_options=new ArrayList<String>();
    final ArrayList<String> city_options=new ArrayList<String>();

    state_options.add("state_1");
    state_options.add("state_2");
    state_options.add("state_3");
    // Here you can also get a cursor and add Strings as options to state_options instead of what i have done

    city_options.add("city_1_state_1");
    city_options.add("city_2_state_1");
    city_options.add("city_3_state_1");
    // Here you can also get a cursor and add Strings as options to city_options instead of what i have done

    ArrayAdapter<String> cityAdapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,city_options);
    city.setAdapter(cityAdapter);   

    ArrayAdapter<String> stateAdapter = new ArrayAdapter<String>(getApplicationContext(),state_options);
    state.setAdapter(stateAdapter);

    state.setOnItemSelectedListener(new OnItemSelectedListener() {

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

            String stateName=state_options.get(position).toString();
                resetCity(stateName);                               
        }

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

        }
   });

现在,

public void resetCity(String stateName)
{      
      city_options.removeAll(city_options);//i haven't checked this.
      if(stateName.eqauls("state_1"))
      {
          city_option.add("city_1_state_1");
          city_options.add("city_2_state_1");
          city_options.add("city_3_state_1");
          //you can also get a cursor and add Strings as options to city_options instead of what i have done
      }
      else if(stateName.eqauls("state_2"))
      {
          city_option.add("city_1_state_2");
          city_options.add("city_2_state_2");
          city_options.add("city_3_state_2");
          // you can also get a cursor and add Strings as options to city_options instead of what i have done
      } 
      else
      {
          city_option.add("city_1_state_3");
          city_options.add("city_2_state_3");
          city_options.add("city_3_state_3");
          //you can also get a cursor and add Strings as options to city_options instead of what i have done
      }

      ArrayAdapter<String> cityAdapter = new ArrayAdapter<String>(getApplicationContext(),city_options);
      city.setAdapter(cityAdapter);
}

这是最简单的示例.您可以从数据库中设置city_options和state_options.然后您可以使用它来填充accoring spinners.

原文链接:https://www.f2er.com/android/313785.html

猜你在找的Android相关文章