mukeshsolanki / country-picker-android

A simple library that displays a beautiful list of all the countries allowing the user to pick the country he wishes and provide details like country code, iso code name,currency and flag.
MIT License
322 stars 135 forks source link

Sorting on the list of countries doesn't work at all #94

Closed surbhi-jungleworks closed 4 years ago

surbhi-jungleworks commented 4 years ago

Subject of the issue

Sorting on the list of countries doesn't work at all

Steps to reproduce

Simply add sort code for Name & you can see it's not working

Expected behaviour

It must be handle sort options

Written Code:

val builder: CountryPicker.Builder = CountryPicker.Builder().with(this) .listener(this).sortBy(CountryPicker.SORT_BY_NAME).canSearch(false)

countryPicker = builder.build()

visionfaysal commented 4 years ago
final CountryPicker picker = CountryPicker.newInstance("Select Country");
        List<Country> countryArrayList = new ArrayList<>();
        countryArrayList.add(new Country("AU", "Australia", "+61", com.ybs.countrypicker.R.drawable.flag_au));
        countryArrayList.add( new Country("GB", "United Kingdom", "+44", com.ybs.countrypicker.R.drawable.flag_gb));
        picker.setCountriesList(countryArrayList);``
mukeshsolanki commented 4 years ago

@surbhi-jungleworks your code seems to be working perfectly for me. Can you share a sample code that I can use to debug?

codex-developers commented 4 years ago

You need to call "sortCountries" in the place when u are setting up the recyclerView, because you are using sort for 'countries' array not for 'searchResults' array.

 @Override public void setupRecyclerView(View sheetView) {
    searchResults = new ArrayList<>();
    searchResults.addAll(countries);

    sortCountries(searchResults);   // ********* ADD THIS LINE ***************

    adapter = new CountriesAdapter(sheetView.getContext(), searchResults,
        new OnItemClickListener() {
          @Override public void onItemClicked(Country country) {
            if (onCountryPickerListener != null) {
              onCountryPickerListener.onSelectCountry(country);
              if (bottomSheetDialog != null) {
                bottomSheetDialog.dismiss();
              }
              if (dialog != null) {
                dialog.dismiss();
              }
              dialog = null;
              bottomSheetDialog = null;
              textColor = 0;
              hintColor = 0;
              backgroundColor = 0;
              searchIconId = 0;
              searchIcon = null;
            }
          }
        },
        textColor);
    countriesRecyclerView.setHasFixedSize(true);
    LinearLayoutManager layoutManager = new LinearLayoutManager(sheetView.getContext());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    countriesRecyclerView.setLayoutManager(layoutManager);
    countriesRecyclerView.setAdapter(adapter);
  }
mukeshsolanki commented 4 years ago

done