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;
}
Have you tried to remove
sortNearby();in your getView method ?You must not call this inside the getView method.