Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7867049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:36:11+00:00 2026-06-03T00:36:11+00:00

I have 2 options of sorting on my ListActivity through a CheckBox . The

  • 0

I have 2 options of sorting on my ListActivity through a CheckBox.
The default sorting is when the CheckBox is unchecked, by sorting the nearby places on the List based on its distance with the user and the other one when the CheckBox is checked, the entry on the List will be sorted alphabetically from A-Z.

So by default, when the user opens the app, the checkbox is unchecked and the ListView should be sorted based on ‘Nearby’.

Both methods I have work perfectly based on the click on the checkbox when I don’t call any of the sort method inside the ListViewAdapter. That means, at first the entries on the List aren’t sorted but when the CheckBox is checked it will sort the list alphabetically, when the it’s unchecked it will be sorted based on ‘Nearby’.

But when I call the sortNearby() method inside the ListViewAdapter, the alphabet sort method doesn’t work at all. The ListView stays sorted based on ‘Nearby’.

What do I need to do to enable the alphabet sorting method update on the ListView when I click the CheckBox?

Here’s how I call the sort method based on a click on the CheckBox and the method themselves:

@Override
    public void onClick(View view) {
        // TODO Auto-generated method stub

        if (view == findViewById(R.id.cb_tab_sort)) {
             if (view.isSelected()){
                    view.setSelected(false);
                    sortNearby();
                    videoLocationAdapter.notifyDataSetChanged();

             } else {
                    view.setSelected(true);
                    sortAZ();
                    videoLocationAdapter.notifyDataSetChanged();
                }        
       }

    }

    public void sortAZ(){
        Arrays.sort(videoLocations, new Comparator<VideoLocation>() {

            @Override
            public int compare(VideoLocation lhs, VideoLocation rhs) {
                return lhs.name.compareTo(rhs.name);
            }
        });
    }

    public void sortNearby(){
        Arrays.sort(videoLocations, new Comparator<VideoLocation>() {

            @Override
            public int compare(VideoLocation lhs, VideoLocation rhs) {

                double lat = location.getLatitude();
                double lng = location.getLongitude();

                double lat1 = lhs.latitude;
                double lng1 = lhs.longitude;

                double lat2 = rhs.latitude;
                double lng2 = rhs.longitude;

                double lhsDistance = countDistance(lat,lng,lat1,lng1);
                double rhsDistance = countDistance(lat,lng,lat2,lng2);
                if (lhsDistance < rhsDistance) 
                    return -1;
                else if (lhsDistance > rhsDistance) 
                    return 1;
                else return 0;
            }
        });
    }

and here’s how I call the sorting method inside the ListViewAdapter

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            convertView = LocationsListActivity.this.getLayoutInflater().inflate(R.layout.listitems, null, true);
        }


        final VideoLocation vidLocation = videoLocations[position];


        //Image
        ImageView v = (ImageView)convertView.findViewById(R.id.image);
        String url = vidLocation.documentary_thumbnail_url;
        v.setTag(url);
        loader.DisplayImage(url, LocationsListActivity.this, v);

        //Title
        TextView titleView = (TextView)convertView.findViewById(R.id.txt_title);
        String title = vidLocation.name;
        titleView.setText(title.toUpperCase());
        Typeface fontRegular = Typeface.createFromAsset(getAssets(), "miso-regular.ttf");
        titleView.setTypeface(fontRegular);

        //Description
        TextView descView = (TextView)convertView.findViewById(R.id.txt_list_desc);
        String desc = vidLocation.text;
        descView.setText(desc);
        Typeface fontLight = Typeface.createFromAsset(getAssets(), "miso-light.ttf");
        descView.setTypeface(fontLight);

        //More
        TextView more = (TextView)convertView.findViewById(R.id.txt_more);
        more.setText(getString(R.string.de_list_more));
        more.setTypeface(fontLight);

        //Distance
        final TextView distanceView = (TextView)convertView.findViewById(R.id.txt_distance);
        distanceView.setTypeface(fontRegular);


        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (locationManager == null) {
            Toast.makeText(LocationsListActivity.this,
                    "Location Manager Not Available", Toast.LENGTH_SHORT)
                    .show();
        }
        location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null)
            location = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {

            double lat = location.getLatitude();
            double lng = location.getLongitude(); 

            double lat2 = roundDown(vidLocation.latitude);
            double lng2 = roundDown(vidLocation.longitude);


            if (countDistance(lat,lng,lat2,lng2)>= 500){
                double kilometer = countDistance(lat,lng,lat2,lng2) /1000;
                int decimalPlaces = 1;
                BigDecimal decimal = new BigDecimal(kilometer);
                decimal = decimal.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
                double new_km= decimal.doubleValue();
                distanceView.setText(new_km+" km");
            }
            else
            {
                int decimalPlaces = 1;
                BigDecimal decimal = new BigDecimal(countDistance(lat,lng,lat2,lng2));
                decimal = decimal.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
                double meter= decimal.doubleValue();
                distanceView.setText(meter +" m");
            }
        }

        locationListener = new LocationListener() {
            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            }

            @Override
            public void onProviderEnabled(String arg0) {
            }

            @Override
            public void onProviderDisabled(String arg0) {
            }

            @Override
            public void onLocationChanged(Location l) {
                location = l;
                locationManager.removeUpdates(this);
                if (l.getLatitude() == 0 || l.getLongitude() == 0) {
                } else {
                    double lat = l.getLatitude();
                    double lng = l.getLongitude(); 

                    double lat2 = roundDown(vidLocation.latitude);
                    double lng2 = roundDown(vidLocation.longitude);


                    if (countDistance(lat,lng,lat2,lng2)>= 500){
                        double kilometer = countDistance(lat,lng,lat2,lng2) /1000;
                        int decimalPlaces = 1;
                        BigDecimal decimal = new BigDecimal(kilometer);
                        decimal = decimal.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
                        double new_km= decimal.doubleValue();
                        distanceView.setText(new_km+" km");
                    }
                    else
                    {
                        int decimalPlaces = 1;
                        BigDecimal decimal = new BigDecimal(countDistance(lat,lng,lat2,lng2));
                        decimal = decimal.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
                        double meter= decimal.doubleValue();
                        distanceView.setText(meter +" m");
                    }
                }
            }
        };

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 1000, 10f, locationListener);
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
        locationtimer = new CountDownTimer(30000, 5000) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (location != null)
                    locationtimer.cancel();
            }

            @Override
            public void onFinish() {
                if (location == null) {
                }
            }
        };
        locationtimer.start();
                sortNearby();
        return convertView;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:36:12+00:00Added an answer on June 3, 2026 at 12:36 am

    Have you tried to remove sortNearby(); in your getView method ?

    You must not call this inside the getView method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some options for pricing using radio buttons. When a user selects the
I have an options column which is serialized. I plan to store user preferences
I have 2 fields for sorting: Product Name and Price plus 2 options: ascending
I want to add some Ajax sorting options to a list. Since my site
I have some objects that have 3 sorting options: quality, quatity and a compare
I have writen options to a <select> using something like Id.innerHTML = <option value='foo'>Foo</option>;
I have an options object in my CS class, and I'd like to keep
I have added options in application.rb: config.autoload_paths += %W(#{config.root}/lib) config.autoload_paths += Dir[#{config.root}/lib/**/] and lib\functions.rb:
I have a ADO.NET/TSQL performance question. We have two options in our application: 1)
I have a collection that is binded to my Listview. I have provided options

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.