0

I am trying to format a list of dates(on user entry) and display in chronological order in a list view.

I am currently able to display them in list view and enter them as an un-formatted date.

I am wanting to format them as follows: DD/MM/YYYY

This is the code i have for retrieving the date and the ID from the db and displaying it in the listView

lessonNotesList class:

    final int VALUE = extras.getInt("studentId");
    mydb = new DBHelper(this);
    final ArrayList id_list = mydb.getAllLessonIds(VALUE);
    ArrayList array_list = mydb.getAllLessonNotes(VALUE);
    //Collections.sort(array_list, Collections.reverseOrder());
    //Collections.sort(array_list);
    ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.list_layout, array_list);
    //Adding the contacts to the list view.
    lessonNote = (ListView)findViewById(R.id.listViewLesson);
    lessonNote.setAdapter(arrayAdapter);
    //Setting an onClickListener to process the user's actions
    lessonNote.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            //Prepare the data from the ListView item to be passed to new activity
            int id_To_Search =Integer.valueOf((String)id_list.get(arg2));
            //Log.d("Id is ^&*: ", String.valueOf(id_To_Search));
            //Create a new intent to open the DisplayContact activity
            Intent lessonList = new Intent(getApplicationContext(), com.example.ltss.dyslexia.app.LessonNotes.class);
            lessonList.putExtra("studentId", VALUE);
            lessonList.putExtra("lessonId", id_To_Search);
            lessonList.putExtras(lessonList);
            startActivity(lessonList);
        }
    });

LessonNotesPage class (displays the lesson note information/allows for user entry)

    String dateLesson = rs.getString(rs.getColumnIndex(DBHelper.DATE));
        if (!rs.isClosed()) {
            rs.close();
        }
        lessonDate.setText(dateLesson);
        lessonDate.setFocusable(true); 
        lessonDate.setClickable(true);// allows user entry 

How do I get the date formatted on user entry and how can i get it sorted in chronological order?

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
ShWhite
  • 151
  • 13

2 Answers2

2

If you want to sort your ArrayList of String dates named array_list try this:

Collections.sort(array_list, new Comparator<String>() {
    DateFormat f = new SimpleDateFormat("DD/MM/YYYY");
    @Override
    public int compare(String o1, String o2) {
        try {
            return f.parse(o1).compareTo(f.parse(o2));
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }
});

If you want to sort ArrayList of Dates is easier (primitive types does not have compareTo()):

Collections.sort(array_list, new Comparator<Date>() {
    @Override
    public int compare(String o1, String o2) {
        return o1.compareTo(o2));
    }
});
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
1

add order by clause in your sql statement

SELECT * FROM Table ORDER BY datetime(dateColumn) DESC

and if you want to reorder when user changes date, use any sorting algorithm and call

adapter.notifyDataSetChanged();
Farhan C K
  • 1,159
  • 18
  • 35