마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

리스트뷰 오름차순 정렬에 관하여 질문드립니다. [closed]

0 추천

아래는 사람들의 이름과 전화번호 위치정보(위도,경도)를 받아와서

이름, 전화번호, 나와의 거리를

리스트뷰에 뿌려주는 소스입니다.

모든 것이 잘 작동하는데,

 

사람이름 순으로 정렬하여 리스트뷰에 나열하는 방법과

거리순으로 정렬하여 리스트뷰에 나열하는 방법에 대하여 고견을 구합니다.

public class MainActivity extends Activity{
     
     
    ArrayList<HashMap<String, Object>> searchResults;
    ArrayList<HashMap<String, Object>> originalValues;
    private CustomAdapter adapter;
    LayoutInflater inflater;
     
    double x,y;
 
    Timer timer;
    LocationManager lm;
    boolean gps_enabled = false;
    boolean network_enabled = false;
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        final EditText searchBox = (EditText) findViewById(R.id.et_search_name);
        final ListView insurListView = (ListView) findViewById(android.R.id.list);
         
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         
         
        String names[] = {
                "K",
                "J",
                "L",
                "O",
                "S",
                        };     
         
        String calls[] = {
                "11111111",
                "22222222",
                "33333333",
                "44444444",
                "55555555",
                        };
 
        Double lati[] = {
                35.000001,
                26.000001,
                67.000001,
                15.000001,
                79.000001,
                        }; 
         
        Double longi[] = {
                129.000001,
                159.000001,
                100.000001,
                131.000001,
                162.000001,
                        };         
 
        originalValues = new ArrayList<HashMap<String, Object>>();
 
        HashMap<String, Object> temp;
 
        int oriname = names.length;
 
        for (int i = 0; i < oriname; i++) {
            temp = new HashMap<String, Object>();
 
                temp.put("name",    names[i]);
                temp.put("call",    calls[i]);
                temp.put("lati",    lati[i]);
                temp.put("longi",   longi[i]);
             
            originalValues.add(temp);
        }
 
        searchResults = new ArrayList<HashMap<String, Object>>(originalValues);
 
        adapter = new CustomAdapter(this,R.layout.list_row, searchResults);
 
        insurListView.setAdapter(adapter);
        searchBox.addTextChangedListener(new TextWatcher() {
 
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String searchString = searchBox.getText().toString();
                int textLength = searchString.length();
                searchResults.clear();
 
                for (int i = 0; i < originalValues.size(); i++) {
                    String insurName = originalValues.get(i).get("name").toString();
                    if (textLength <= insurName.length()) {
                         
                        if (searchString.equalsIgnoreCase(insurName.substring(0, textLength)))
                            searchResults.add(originalValues.get(i));
                    }
                }
                 
                adapter.notifyDataSetChanged();
            }
 
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                 
            }
 
            public void afterTextChanged(Editable s) {
 
            }
        });
         
/*gps part*/   
         
         lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
         gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
         network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
 
         if (!gps_enabled && !network_enabled) {
             Context context = getApplicationContext();
             int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, "nothing is enabled", duration);
            toast.show();
         }
         
         if (gps_enabled)
             lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                     locationListenerGps);
         if (network_enabled)
             lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                     locationListenerNetwork);
         
         
    }
      
    private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>> {
 
        public CustomAdapter(Context context, int textViewResourceId,
                ArrayList<HashMap<String, Object>> Strings) {
 
            super(context, textViewResourceId, Strings);
        }
 
        private class ViewHolder {
            TextView name;
            TextView call;
            TextView distance;
        }
 
        ViewHolder viewHolder;
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
 
            double sLat = Double.valueOf(searchResults.get(position).get("lati").toString());
            double sLng = Double.valueOf(searchResults.get(position).get("longi").toString());
 
            double eLat = Double.valueOf(x);
            double eLng = Double.valueOf(y);
             
            if (x==0){
                sLat = 0;
                sLng = 0;
            }
             
            String distance = SocialUtil.calcDistance(sLat, sLng, eLat, eLng);
             
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.list_row, null);
                viewHolder = new ViewHolder();
 
                viewHolder.name = (TextView) convertView.findViewById(R.id.name);
                viewHolder.call = (TextView) convertView.findViewById(R.id.call);
                viewHolder.distance = (TextView) convertView.findViewById(R.id.distance);
 
                convertView.setTag(viewHolder);
 
            } else
                viewHolder = (ViewHolder) convertView.getTag();
 
            final int pos = position;
 
            viewHolder.name.setText(searchResults.get(position).get("name").toString());
            viewHolder.call.setText(searchResults.get(position).get("call").toString());
            viewHolder.distance.setText(distance);     
             
            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);
             
            adapter.notifyDataSetChanged();
             
            Context context = getApplicationContext();
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, "gps enabled "+x + "\n" + y, duration);
            toast.show();
        }
 
        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);
             
            adapter.notifyDataSetChanged();
 
            Context context = getApplicationContext();
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, "network enabled"+x + "\n" + y, duration);
            toast.show();
        }
 
        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;
        }
      
    }
}

 

질문을 종료한 이유: 해결했습니다. 답변자분께 감사드립니다.^^
동네영웅 (220 포인트) 님이 2014년 9월 18일 질문
동네영웅님이 2014년 9월 22일 closed

1개의 답변

0 추천
Collections.sort(List<?> list)라는 함수로 string이나 숫자 정렬하실 수 있습니다.
hahohehi (1,250 포인트) 님이 2014년 9월 19일 답변
...