현재 나의 지점에서 대상 지점까지 거리를 리스트뷰에 나타내려고 하고 있습니다.
X, Y는 현재 나의 좌표구요...
처음 실행하면 X, Y좌표가 0, 0 으로 계산되어 표시 됩니다.
(디바이스에서 실행했을때, 몇 초 지나면 X, Y 좌표가 제대로 불러와집니다.)
그렇다면, X, Y 좌표가 제대로 불러와졌을때, 거리를 계산해서
리스트에 표시가 되어야하는데, 처음 0, 0으로 계산된 값만 표시가 됩니다...
그래서 notifyDataSetChanged() 를 사용하여, 좌표값이 제대로 불러왔을 때
갱신하려고 했으나 리스트가 전혀 갱신이 되지 않습니다.
clear(), addAll() 도 같이 써봤는데, 이렇게 하면,
리스트뷰에 0, 0으로 계산된 값만 표시되다가
제대로 된 좌표가 불러와지면, 리스트가 사라져버립니다..
몇 일째 이걸로 고민하고 있는데, 부디 도움의 손길을 부탁드립니다.....
import java.util.ArrayList;
public class MainActivity extends Activity {
double x,y;
Timer timer;
LocationManager lm;
boolean gps_enabled = false;
boolean network_enabled = false;
private CustomAdater adapter;
private ArrayList<MyItem> arrayItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
double eLat = Double.valueOf(x);
double eLng = Double.valueOf(y);
arrayItems = new ArrayList<MyItem>();
arrayItems.add(new MyItem("Tom01", SocialUtil.calcDistance(35.000001, 129.000001, eLat, eLng)));
arrayItems.add(new MyItem("Tom02", SocialUtil.calcDistance(26.000001, 159.000001, eLat, eLng)));
arrayItems.add(new MyItem("Tom03", SocialUtil.calcDistance(67.000001, 100.000001, eLat, eLng)));
arrayItems.add(new MyItem("Tom04", SocialUtil.calcDistance(15.000001, 131.000001, eLat, eLng)));
arrayItems.add(new MyItem("Tom05", SocialUtil.calcDistance(79.000001, 162.000001, eLat, eLng)));
adapter = new CustomAdater(this, R.layout.list_row, arrayItems);
ListView list = (ListView) this.findViewById(android.R.id.list);
list.setAdapter(adapter);
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListenerGps);
if (network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListenerNetwork);
timer=new Timer();
timer.schedule(new GetLastLocation(), 20000);
}
public class CustomAdater extends BaseAdapter {
Context context;
int itemlayout;
ArrayList<MyItem> arrayItems;
LayoutInflater inflater;
public CustomAdater(Context c, int sublayout, ArrayList<MyItem> list) {
this.context = c;
this.itemlayout = sublayout;
this.arrayItems = list;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return arrayItems.size();
}
@Override
public Object getItem(int position) {
return arrayItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
if (convertView == null) {
convertView = inflater.inflate(itemlayout, parent, false);
}
TextView name = (TextView) convertView.findViewById(R.id.list_name);
TextView dis = (TextView) convertView.findViewById(R.id.list_distance);
name.setText(arrayItems.get(position).mname);
dis.setText(arrayItems.get(position).mdis);
return convertView;
}
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer.cancel();
x =location.getLatitude();
y = location.getLongitude();
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
arrayItems.clear();
arrayItems.addAll(arrayItems)
adapter.notifyDataSetChanged();
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer.cancel();
x = location.getLatitude();
y = location.getLongitude();
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
arrayItems.clear();
arrayItems.addAll(arrayItems)
adapter.notifyDataSetChanged();
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
public static class SocialUtil {
public static String calcDistance(double lat1, double lon1, double lat2, double lon2){
double EARTH_R, Rad, radLat1, radLat2, radDist;
double distance, ret;
EARTH_R = 6371000.0; Rad = Math.PI/180; radLat1 = Rad * lat1;
radLat2 = Rad * lat2; radDist = Rad * (lon1 - lon2);
distance = Math.sin(radLat1) * Math.sin(radLat2);
distance = distance + Math.cos(radLat1) * Math.cos(radLat2) * Math.cos(radDist);
ret = EARTH_R * Math.acos(distance);
double rslt = Math.round(Math.round(ret) / 1000);
String result = rslt + " km";
if(rslt == 0) result = Math.round(ret) +" m";
return result;
}
}
public class MyItem {
String mname, mdis;
public MyItem(String name, String dis){
mname = name;
mdis = dis;
}
}
}