Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Android studio sorting listview that contain a textview in which i m showing distance and distance is calculating at runtime ie not from arraylist or Sqlite , I want to sort list view based on distance and also restrict listview to show only 50 items

What I have tried:

Java
list.setAdapter(new BaseAdapter() {
    // @Override
    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        View v = getLayoutInflater().inflate(R.layout.single_row, null);
        TextView i,n,e,p;
        // i=(TextView)v.findViewById(R.id.textView1);
        n=(TextView)v.findViewById(R.id.textView2);
        e=(TextView)v.findViewById(R.id.textView3);
        p=(TextView)v.findViewById(R.id.textView6);
        Employee emp = employees.get(arg0);
        // i.setText(emp.getId());
        e.setText(emp.getAddress());
        n.setText(emp.getName());
        //new code for lat distance
        double lat =Double.parseDouble(emp.getLat());
        double log = Double.parseDouble(emp.getLog());
        /*  Intent mIntent = getIntent();
            String flatvalue = mIntent.getStringExtra("flat");
            String flogvalue = mIntent.getStringExtra("flog");
            double flat =Double.parseDouble(flatvalue.toString());
            double flog = Double.parseDouble(flogvalue.toString());*/
        if(gps.canGetLocation()){
            double flat = gps.getLatitude();
            double flog = gps.getLongitude();
            double dis = geoCoordToMeter(flat, flog, lat,log);
            int distnace =(int) dis;
            p.setText(String.valueOf(distnace) + " km" );
        }
        return v;
    }
Posted
Updated 8-Sep-16 0:27am
v2

1 solution

Add your elements to a List<t>, then use Collections (Java Platform SE 6)[^].Sort on that list. You can then extract the items you want and add them to the Listview, or use a derived list as source.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900