0

I would like to sort the following listview according to "time". Is there any way i can do that on the following code? So for example that 19:15 appears above 20:30 etc...

I would be very thankful to any kind of help!

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    List<String> listTime = new ArrayList<String>();
    Collections.sort(listTime);
    final List<String> idsList = new ArrayList<>();
    ArrayAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, listTime);
    final ListView Time = (ListView) findViewById(R.id.Listtime);

    for (final DataSnapshot ds : dataSnapshot.getChildren()) {
        String user = ds.child("UserName").getValue(String.class);
        String arrival = ds.child("Arrival").getValue(String.class);
        String departure = ds.child("Departure").getValue(String.class);
        String time = ds.child("Time").getValue(String.class);
        String TripID = ds.child("TripID").getValue(String.class);
        idsList.add(TripID);
        String Trips = TextUtils.join("  |  ", new String[]{time, user,  departure, arrival});
        listTime.add(Trips);
        Time.setAdapter(adapter);
        Time.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(AvailableRides.this,RideSelected.class);
                String entry = (String) adapterView.getItemAtPosition(i);
                TextView textView4 = findViewById(R.id.textView30);
                String user = textView4.getText().toString();
                String TripID = idsList.get(i);
                intent.putExtra(EXTRA_MESSAGE10,user);
                intent.putExtra(EXTRA_MESSAGE8, entry);
                intent.putExtra(EXTRA_MESSAGE9, TripID);
                startActivity(intent);
            }
        });
    }
}
Michael
  • 3,093
  • 7
  • 39
  • 83
bwen
  • 3
  • 1
  • 7
  • You have to save date in Date format instead of String .Then Collections.sort will sort the date. – Praveen Rawat Apr 11 '18 at 12:41
  • Does that mean i only have to change from "String time" to "Date time"? – bwen Apr 11 '18 at 12:50
  • Here is an link [https://stackoverflow.com/questions/15085608](https://stackoverflow.com/questions/15085608/how-to-sort-an-arraylist-of-date-in-ascending-and-descending-order-which-is-in-s) – Praveen Rawat Apr 11 '18 at 12:54

1 Answers1

0

First I would recommend to create a wrapper class for all of these information like Trip.

public class Trip {
  private String username;
  private Date arrival;
  private String departure;
  private Date time;
  private String tripId;

  // create getter + setter

  // you also can overwrite toString to get all information
}

For date / time compares you have to convert the String into a Date object.

Now you have two options:

  1. Implement comparable in your new created wrapper class:

    public class Trip implements Comparable<Trip> {
    

    and implement the method:

    public int compareTo(Trip trip) {
        // care maybe nullpointer can occure here -> check it brefore
        return time.compareTo(trip.getTime());
    }
    
  2. Create a comparator. Then you have to change the sort call like:

    Collections.sort(listTime, yourComparator);
    

    And create the comparator:

    public class TripComparator implements Comparator<Trip> {
        public int compare(Trip trip1, Trip trip2) {
            // care maybe nullpointer can occure here -> check it before
            return trip1.getTime().compareTo(trip2.getTime());
        }
    }
    

You also can implement Parcelable inside the wrapper class. Then you can pass this object to another activity or whatever.

ColdFire
  • 6,764
  • 6
  • 35
  • 51
beeb
  • 1,615
  • 1
  • 12
  • 24