Zero
Zero

Reputation: 1904

Populating a spinner with specific entries from an array

I am working on an application for Android. For this I am making an Activity in which you select your country and then a spot in that country. I have one spinner that contains a list of all available countries. Now, what I want it to do is get the country that has been selected, then filter a list of spots that I have for the items that start with the country that has been selected. Then it should put the spots for the selected country into a different spinner. Just for clarity, the list of countries is just a list of countries, and the list of spots looks like:

And so on.

This is what I thought the code should work like:

I tried to achieve this with the following code:

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    String selectedCountry = parent.getItemAtPosition(pos).toString();
    ArrayList<CharSequence> arraylist = new ArrayList<CharSequence>();
    arraylist.addAll(R.array.spots_array);
    ArrayList<CharSequence> arraylist2 = new ArrayList<CharSequence>();
    for (i=0; i<arraylist.size(); i++) {
        String delimiter = " - ";
        if ((arraylist(i).split(delimiter)).equals(selectedCountry)) {
            arraylist2.add(arraylist(i).string.substring(string.lastIndexOf('-') + 1));
        }

    }
    ArrayAdapter<CharSequence> arrayAdapter2 = ArrayAdapter.createFromResource(this,  arraylist2<CharSequence>, android.R.layout.simple_spinner_item);
    arrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(arrayAdapter2);
    spinner2.setOnItemSelectedListener(this);
}

But it gives several errors:

I am still relatively new to Android, and am having a lot of trouble getting this working. Can anybody please help me out?

Upvotes: 0

Views: 675

Answers (2)

florianmski
florianmski

Reputation: 5643

There is a lot of little mistakes in your code :

  • To access an element in an arraylist use the get(position) method
  • When you add your "spot_array", you actually add the id of the resource, not the array itself (see here)

Here is your code updated, it should works or may need some tweaks

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String selectedCountry = parent.getItemAtPosition(pos).toString();
        List<CharSequence> arraylist = new ArrayList<CharSequence>();
        arraylist.addAll(Arrays.asList(getResources().getTextArray(R.array.spots_array)));
        List<CharSequence> arraylist2 = new ArrayList<CharSequence>();
        String delimiter = " - ";
        for (int i=0; i<arraylist.size(); i++) {
            String country = arraylist.get(i).toString();
            if (country.contains(selectedCountry)) {
                arraylist2.add(country.substring(country.lastIndexOf('-') + 2));
            }

        }
        ArrayAdapter<CharSequence> arrayAdapter2 = ArrayAdapter.createFromResource(this,  android.R.id.text1, android.R.layout.simple_spinner_item);
        arrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(arrayAdapter2);
        spinner2.setOnItemSelectedListener(this);
    }

Upvotes: 3

Chupamobile Evangelist
Chupamobile Evangelist

Reputation: 1116

You have several errors in your code.

Firstly, the method addAll of the ArrayList must take as an argument a Collection. You are passing an Android array id R.array.spots_array; bear in mind that the Android ids are integers.

The usually method to fetch a string array from Android resources is (inside an activity):

String[] myArray = getResources().getStringArray(R.array.spots_array);

Second error: you should access the ArrayList elements by calling the method get(position) , not directly (arraylist(position)). Something like arraylist.get(position).

Third error:

arraylist2.add(arraylist(i).string.substring(string.lastIndexOf('-') + 1));

should simply be arraylist2.add(arraylist.get(i)); for adding one list element to another.

More on ArrayLists can be found here.

Upvotes: 0

Related Questions