【发布时间】:2016-07-15 23:51:03
【问题描述】:
我正在使用教程项目,无法理解如何限制拉取的项目数。
【问题讨论】:
标签: android firebase firebase-realtime-database firebaseui
我正在使用教程项目,无法理解如何限制拉取的项目数。
【问题讨论】:
标签: android firebase firebase-realtime-database firebaseui
您可以使用DatabaseReference 或Query 初始化您的FirebaseRecyclerAdapter。当您使用DatabaseReference 时,适配器将显示该位置的所有数据。例如。 (来自FirebaseUI docs):
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
DatabaseReference chatRef = ref.child("chat_messages");
mAdapter = new FirebaseListAdapter<Chat>(this,
Chat.class,
android.R.layout.two_line_list_item,
chatRef) {
@Override
protected void populateView(View view, Chat chatMessage, int position) {
((TextView)view.findViewById(android.R.id.text1)).setText(chatMessage.getName());
((TextView)view.findViewById(android.R.id.text2)).setText(chatMessage.getText());
}
};
messagesView.setAdapter(mAdapter);
要仅显示最后 5 条聊天消息,您需要创建一个查询并将其传递给适配器:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
DatabaseReference chatRef = ref.child("chat_messages");
Query recentMessages = chatRef.limitToLast(5);
mAdapter = new FirebaseListAdapter<Chat>(this,
Chat.class,
android.R.layout.two_line_list_item,
recentMessages) {
@Override
protected void populateView(View view, Chat chatMessage, int position) {
((TextView)view.findViewById(android.R.id.text1)).setText(chatMessage.getName());
((TextView)view.findViewById(android.R.id.text2)).setText(chatMessage.getText());
}
};
messagesView.setAdapter(mAdapter);
您可以使用 Firebase 数据库查询做更多事情。阅读documentation on sorting and filtering data了解更多信息。
【讨论】: