0

I have a firebase database. In this database, event details are inserted. One of the details is the date of the event.

I'm exporting the database to a ListView in my activity. But I want to order the event list by the date of the events that the first event will be the nearest in terms of date.

Example of a date (String): "26.07.2016" .

(dd/mm/yyyy).

This is my current code:

 private void populateEventList()
{
    firebaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            ArrayList<String> titles = new ArrayList<String>();
            ArrayList<String> subTitles = new ArrayList<String>();
            ArrayList<EventClass> eventList = new ArrayList<EventClass>();
            for (DataSnapshot user : snapshot.child("users").getChildren()) {

                String event_title = user.child("event/event_title").getValue().toString();
                titles.add(event_title);
                String event_date = user.child("event/event_date").getValue().toString();
                subTitles.add(event_date);
                String event_content = user.child("event/event_content").getValue().toString();
                String age_limit = user.child("event/age_limit").getValue().toString();
                String event_hour = user.child("event/event_hour").getValue().toString();
                String location_left = user.child("location_left").getValue().toString();
                String location_right = user.child("location_right").getValue().toString();

                EventClass oneEvent = new EventClass(event_title, event_date, event_content, age_limit, event_hour, location_left, location_right);
                eventList.add(oneEvent);

            }
            ArrayAdapter adapter = new ArrayAdapter(Event_List_Activity.this, android.R.layout.simple_list_item_1, titles) {

                @Override
                public View getView(int position, View convertView,
                                    ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);

                    TextView textView = (TextView) view.findViewById(android.R.id.text1);
                    textView.setTextColor(Color.WHITE);

                    return view;
                }
            };
            ListView listView = (ListView) findViewById(R.id.listView1);

            listView.setAdapter(adapter);


        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            String message = "Error";
            Toast.makeText(Event_List_Activity.this, message, Toast.LENGTH_SHORT).show();
        }
    });

}
Gilad Neiger
  • 319
  • 1
  • 3
  • 14
  • Collections.sort the Arraylist before adding the adapter to the ListView. – OneCricketeer Mar 26 '16 at 17:31
  • I need to sort by date... How to do this? – Gilad Neiger Mar 26 '16 at 17:36
  • The second parameter to `Collections.sort` can be a custom `Comparator` that you can sort Date strings with. The problem, though, is if you sort the eventlist you also need to sort the titles and subtitles in the same order. Why not make all those fields into a single Java object instead of 3 separate lists? – OneCricketeer Mar 26 '16 at 17:41
  • You can also make `EventClass implements Comparable` and implement the sorting there. You still have the 3 list problem, though. – OneCricketeer Mar 26 '16 at 17:43
  • @cricket_007 ok, I'm trying to use your advices. And I have another question: If I'm using `Collections.sort` for sorting the list by date, It will know how to sort it that the most close event will be on the top of the list and the most far event will be on the end of the list? – Gilad Neiger Mar 26 '16 at 18:29
  • Only if you have the strings in yyyy/mm/dd format and sort into ascending order because then it will sort by alphabetical ordering. – OneCricketeer Mar 26 '16 at 18:34
  • @cricket_007 can you please write me a code example? It will help me to understand... – Gilad Neiger Mar 26 '16 at 18:45
  • You just have to store the data differently. If you want to sort data using Firebase queries, then code samples are here. https://www.firebase.com/docs/web/guide/retrieving-data.html. If you want to sort an ArrayList of Java objects, that is here http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property – OneCricketeer Mar 26 '16 at 18:57
  • Can you add the JSON at `firebaseRef` to the question? You can easily get this by clicking the Export button in your Firebase adapter and it allows us to better follow what your code is doing and potentially use the JSON in our answer or in reproducing the problem. – Frank van Puffelen Mar 26 '16 at 22:47

0 Answers0