gpt4 book ai didi

java - Android:向 ListView 中显示的项目的数组列表添加日期格式和排序

转载 作者:行者123 更新时间:2023-12-01 22:18:28 27 4
gpt4 key购买 nike

我正在尝试格式化日期列表(在用户输入时)并在 ListView 中按时间顺序显示。

我目前可以在 ListView 中显示它们并将它们作为未格式化的日期输入。

我想将它们格式化如下:DD/MM/YYYY

这是我从数据库检索日期和 ID 并将其显示在 listView 中的代码

lessonNotesList 类:

    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)笔记信息/允许用户输入)

    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

如何获取用户输入时的日期格式以及如何按时间顺序排序?

最佳答案

如果您想对名为 array_listString 日期的 ArrayList 进行排序,请尝试以下操作:

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);
}
}
});

如果你想对 DatesArrayList 进行排序会更容易(基本类型没有 compareTo()):

Collections.sort(array_list, new Comparator<Date>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2));
}
});

关于java - Android:向 ListView 中显示的项目的数组列表添加日期格式和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30463573/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com