pheromix
pheromix

Reputation: 19347

How to filter a spinner's string-array adapter?

There are two spinners , say spinner1 and spinner2, having both string-array adapters as their adapters. The business logic of the application is that spinner2 depends on the selected value of spinner1. So how to filter spinner2's data from the selected item of spinner1 ? For example adapter 1 has strin-array :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="region"> // for the label displayed
        <item>Analamanga</item>
        <item>Itasy</item>
    </string-array>
    <string-array name="region_id"> // this is for the actual value of the selected label
        <item>1</item>
        <item>2</item>
    </string-array>
</resources>

Adapter 2 has string-array :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="district"> // for the label displayed
        <item>Central</item>
        <item>Boeny</item>
    </string-array>
    <string-array name="district_id"> // this is for the actual value of the selected label
        <item>1</item>
        <item>2</item>
    </string-array>
</resources>

Upvotes: 0

Views: 4360

Answers (1)

joao2fast4u
joao2fast4u

Reputation: 6892

Use an HashTable<Integer,ArrayList<String>> to map your RegionSpinner and DistrictSpinner values.

This HashTable will have the positions of the selected items on RegionSpinner as keys and the ArrayList<String> that will feed your DistrictSpinner adapter as values.

Then, when you select an item on RegionSpinner, you set a new FilterSpinnerAdapter (an Adapter that extends ArrayAdapter) on DistrictSpinner, so the values on it change dynamically.

The ArrayList<String> that will be given to your new Adapter will be returned by your HashTable, according to the position you selected on RegionSpinner.

Here's the code:

public class SpinnerActivity extends Activity{

Spinner spinnerRegion, spinnerDistrict;
int selectionCount=0;
Hashtable<Integer,ArrayList<String>> spinnerValues;

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

    ArrayList<String> regions = new ArrayList<String>();
    regions.add("Analamanga");
    regions.add("Itasy");
    regions.add("ThirdRegion");

    ArrayList<String> analamangaDistricts = new ArrayList<String>();
    analamangaDistricts.add("Boeny");

    ArrayList<String> itasyDistricts = new ArrayList<String>();
    itasyDistricts.add("Central");

    ArrayList<String> thirdRegionDistricts = new ArrayList<String>();
    thirdRegionDistricts.add("District1");
    thirdRegionDistricts.add("District2");
    thirdRegionDistricts.add("District3");

    spinnerValues = new Hashtable<Integer, ArrayList<String>>();

    spinnerValues.put(0, analamangaDistricts);
    spinnerValues.put(1, itasyDistricts);
    spinnerValues.put(2, thirdRegionDistricts);

    spinnerRegion = (Spinner) findViewById(R.id.spinner_region);
    if(spinnerRegion != null) {

        FilterSpinnerAdapter regionadapter = new FilterSpinnerAdapter(getApplicationContext(), R.layout.layout_spinner_item, regions);

        regionadapter.setDropDownViewResource(R.layout.layout_simple_spinner_dropdown_item);
        spinnerRegion.setAdapter(regionadapter);
        spinnerRegion.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long itemId) {
                    FilterSpinnerAdapter newDistrictAdapter = new FilterSpinnerAdapter(getApplicationContext(), R.layout.layout_spinner_item, spinnerValues.get(position));
                    newDistrictAdapter.setDropDownViewResource(R.layout.layout_simple_spinner_dropdown_item);
                    spinnerDistrict.setAdapter(newDistrictAdapter);         
            }

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

    }

    spinnerDistrict = (Spinner) findViewById(R.id.spinner_district);
    if(spinnerDistrict != null) {

        FilterSpinnerAdapter districtadapter = new FilterSpinnerAdapter(getApplicationContext(), R.layout.layout_spinner_item, analamangaDistricts);

        districtadapter.setDropDownViewResource(R.layout.layout_simple_spinner_dropdown_item);
        spinnerDistrict.setAdapter(districtadapter);
        spinnerDistrict.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long itemId) {
                //do whatever you want here
            }

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

    }
}

public class FilterSpinnerAdapter extends ArrayAdapter<String> {
    public FilterSpinnerAdapter(Context context, int resource, ArrayList<String> ys) {
        super(context, resource, ys);
    }

    @Override
    public int getCount() {
        // - 1 so that the hint (last item) isn't shown
        return super.getCount();
    }

    @Override
    public String getItem(int position) {
        return super.getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

}

}

Update: R.layout.layout_spinner_item is the layout for each drop_down item on the spinner. It contains a simple TextView. Like this:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerTitle" android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:padding="5dp" android:textStyle="bold" />

I hope to have helped you. :D

Upvotes: 2

Related Questions