gpt4 book ai didi

java - 如何在一个数组列表中对两个不同的模型进行排序?

转载 作者:行者123 更新时间:2023-12-02 00:07:40 26 4
gpt4 key购买 nike

我正在使用调用日志的数组列表和短信日志模型类创建 RecyclerView。现在我想按日期对列表进行排序。下面的代码返回该数组列表return logObjectList。抱歉我的英语不好。

```private List<BaseModel> getCallLog() {
List<BaseModel> logObjectList;
logObjectList = new ArrayList<>();
@SuppressLint("MissingPermission") Cursor cursor = getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI,
null, null, null, CallLog.Calls.DATE + " DESC ");
int number = Objects.requireNonNull(cursor).getColumnIndex("number");
int type = cursor.getColumnIndex("type");
int date = cursor.getColumnIndex("date");
int duration = cursor.getColumnIndex("duration");
while (cursor.moveToNext()) {
CallModel log1 = new CallModel(getActivity());
log1.setNumber(cursor.getString(number));
log1.setType(cursor.getInt(type));
log1.setDuration(cursor.getInt(duration));
log1.setDate(cursor.getLong(date));
logObjectList.add(log1); }

@SuppressLint("MissingPermission") Cursor cursor1 = Objects.requireNonNull(getActivity()).getContentResolver().query(Telephony.Sms.CONTENT_URI,
null,null,null,Telephony.Sms.DATE + " DESC ");
int smsDate = Objects.requireNonNull(cursor1).getColumnIndexOrThrow(Telephony.Sms.DATE);
int smsNumber = cursor1.getColumnIndexOrThrow(Telephony.Sms.ADDRESS);
int smsType = cursor1.getColumnIndexOrThrow(Telephony.Sms.TYPE);
while (cursor1.moveToNext()){
SmsModel log2 = new SmsModel(getActivity());
log2.setNumber(cursor1.getString(smsNumber));
log2.setDate(cursor1.getLong(smsDate));
log2.setType(cursor1.getInt(smsType));
logObjectList.add(log2);
}
cursor.close();
cursor1.close();
return logObjectList;
} ```

现在我得到的结果是这样的:

1-541-754-3010         19-09-28
0 min 58 secs 17:17:54

+49-89-636-48018 19-09-28
1 min 22 secs 17:16:36

47-73512451 19-09-27
3 min 23 secs 09:25:34

73505073 19-09-26
2 min 02 secs 10:29:54

80046004 19-09-25
0 min 0 secs 13:21:00

1414 19-09-28
Received message 14:25:25

47-73512451 19-09-27
Received message 13:00:12

73505073 19-09-26
Sent message 16:03:17

但我想使用这样的长日期来排序该列表:

1-541-754-3010         19-09-28
0 min 58 secs 17:17:54

1414 19-09-28
Received message 14:25:25

+49-89-636-48018 19-09-28
1 min 22 secs 13:16:36

47-73512451 19-09-27
Received message 13:00:12

47-73512451 19-09-27
3 min 23 secs 09:25:34

73505073 19-09-26
Sent message 16:03:17

73505073 19-09-26
2 min 02 secs 10:29:54

80046004 19-09-25
0 min 0 secs 13:21:00

最佳答案

我想我找到了解决方案。

Collections.sort(logObjectList, (t1, t2) -> {
long date1 = t1 instanceof CallModel ? ((CallModel) t1).getDate() : ((SmsModel) t1).getDate();
long date2 = t2 instanceof CallModel ? ((CallModel) t2).getDate() : ((SmsModel) t2).getDate();
return Long.compare(date2,date1);
});

关于java - 如何在一个数组列表中对两个不同的模型进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58145027/

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