knvchowdary
knvchowdary

Reputation: 81

Sorting the ListView elements and according to that an other String Array also sorting

I have one ListView & two String Arrays For example One String Array have all countries that is Adopted with ListView & other string Array have their capitals,I'm using the TextWathcher & when it is enter the data in EditText when I click on the ListView,according to that Capitals String element also sorting & display the relevant element should display by PopupWindow

adapter_countries = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,allCountries_list);//this is Listview & have coutries      `array`
all_countries_list_view.setAdapter(adapter_countries);
all_countries_list_view.setOnItemClickListener(this);

Upvotes: 0

Views: 60

Answers (1)

Alejandro Cumpa
Alejandro Cumpa

Reputation: 2363

How about creating a subclass that include the country and capital, and make an arrayList from those objects, so you will have to sort just for the countries and the capitals will be asociated to the same item.

public class MyObject
    {
        String country;
        String capital;

        public MyObject()
        {
            this.country = "";
            this.capital = "";
        }
        public MyObject(String newCountry, String newCapital)
        {
            this.country = newCountry;
            this.capital = newCapital;
        }

        public String getCapital()
        {
          return this.capital;
        }
        public String getCountry()
        {
            return this.country;
        }
    }

and then make an arraylist of this new object:

ArrayList<MyObject> myArray = new ArrayList<MyObject>();

fill the array, and when you sort, just made the same sorting you're using for the countries but with myArray.get(index).getCountry()

Upvotes: 1

Related Questions