我有 2 个选项可以ListActivity
通过CheckBox
. 默认排序是在CheckBox
未选中时,通过根据与用户的距离对列表中的附近地点进行排序,在选中时对CheckBox
列表中的条目进行排序,列表中的条目将从AZ开始按字母顺序排序。
因此,默认情况下,当用户打开应用程序时,该复选框未选中,ListView 应根据“附近”进行排序。
当我不调用ListViewAdapter
. 这意味着,首先,列表中的条目未排序,但在CheckBox
检查时,它将按字母顺序对列表进行排序,当它未选中时,它将根据“附近”进行排序。
sortNearby()
但是当我在里面调用方法时ListViewAdapter
,字母排序方法根本不起作用。ListView 保持基于“附近”的排序。
单击 CheckBox 时,我需要做什么才能在 ListView 上启用字母排序方法更新?
以下是我如何根据单击 CheckBox 和方法本身来调用排序方法:
@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;
}
});
}
这是我在 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;
}