我有以下方法来查询聊天活动的onCreate方法中的项目列表,并将其设置为我的适配器:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_chat,container,false);
ListView chatListView = (ListView) rootview.findViewById(R.id.chatListView);
arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, messages);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Message");
query.setLimit(15);
query.orderByAscending("createdAt");
query.whereLessThanOrEqualTo("createdAt", new Date());
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if(objects.size() > 0){
messages.clear();
for (ParseObject message : objects){
messages.add(message.getString("sender") + ": " + message.getString("content"));
}
arrayAdapter.notifyDataSetChanged();
}
} else {
// Something is wrong
}
}
});
chatListView.setAdapter(arrayAdapter);
我得到的结果是前15个数据,而不是最新的15个数据。
我该如何解决?
最佳答案
使用desc命令代替asc命令:
query.orderByAscending("createdAt");
用这个:
query.addDescendingOrder("createdAt");