我有两个选项可以通过ListActivity
对CheckBox
进行排序。
默认排序是在取消选中CheckBox
时,通过根据与用户的距离对列表中的附近位置进行排序,在检查CheckBox
时对另一个位置进行排序,列表中的条目将是从AZ按字母顺序排序。
因此,默认情况下,当用户打开应用程序时,将取消选中该复选框,并根据“附近”对ListView进行排序。
当我没有在ListViewAdapter
内调用任何排序方法时,基于单击复选框,我完全可以正常工作。这意味着,首先列表中的条目没有排序,但是当CheckBox
被选中时,它将按字母顺序对列表进行排序,当未选中它时,它将根据“附近”进行排序。
但是当我在sortNearby()
内调用ListViewAdapter
方法时,字母排序方法根本不起作用。 ListView基于“附近”保持排序。
单击CheckBox时,如何在ListView上启用字母排序方法更新?
以下是基于CheckBox和方法本身的点击来调用sort方法的方法:
@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;
}
答案 0 :(得分:1)
您是否尝试在getView方法中删除sortNearby();
?
你不能在getView方法中调用它。