Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

android - (dynamic) Multiple spinners state/city

I have to develop an android view such that i have 2 spinner controls in it, one for state and the second for cities.

My question is, how can I populate the city spinner automatically whenever a state is selected?

What's the logic behind it?

My 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>

My 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>

And my activity.java file:

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

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Example:

    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(), android.R.layout.simple_spinner_item,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) {

        }
   });

Now,

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(), android.R.layout.simple_spinner_item,city_options);
      city.setAdapter(cityAdapter);
}

This is the simplest example.You can set your city_options and state_options from your database.and then you can use it for populating accoring spinners.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...