I was trying to implement Firebase in android, I have few queries in that, I would be glad if you would help me into this. Getting bit confused in it. So here is my question.
I am showing Firebase chat in listview
in which I want to only load 1st 100 messages from firebase after that User will do pull-to-refresh
then load 1st 200 messages and so on.
For that I am looking in limitToFirst()
and limitToLast()
not getting idea which one is better.
Can anyone please help.
I did almost same thing in this way:
In your class (I assume that is an Activity), create a member variable of Query
type:
private Query queryChat;
In the onStart
method, start doing the query:
@Override
public void onStart() {
super.onStart();
// initialize your value event listener
queryChat= mFirebaseDatabaseReference.
.orderByChild("timestamp")
.limitToLast(pageSize);
queryChat.addListenerForSingleValueEvent(yourValueEventListener);
}
pageSize
is an int
variable and here has to be set to 100.
Doing in this way, you are first making an ascending order of your messages (so, the first messages in the resulting ordered list are the oldest), and then you are picking the last pageSize
messages (so, the pageSize
last in order of time). So, you will get the first (in order of time, starting from the newest) pageSize
messages.
I'm supposing that you already have implemented a ValueEventListener
here. Also, timestamp
is a field in firebase message in which you stored the Timestamp in milliseconds of your message.
Now, define a method in your class like this:
private void loadMoreItems() {
queryChat= mFirebaseDatabaseReference.
.orderByChild("timestamp")
.endAt(messageList.get(messageList.size()-1).getTimestamp())
.limitToLast(pageSize);
queryChat.addListenerForSingleValueEvent(yourValueEventListener);
}
The messageList
here is an ArrayList that stores the chat messages, and is filled inside the ValueEventListener, when you get the messages from firebase.
Here, I'm telling to firebase to do the ascending order (as before), but picking only those items with timestamp <= the last item already visible to the user.(.endAt(messageList.get(messageList.size()-1).getTimestamp())
is telling to firebase exactly this), and finally limitToLast
as before.
And now you can call this method (loadMoreItems
) whenever you want (in your case, when the user do pull to refresh).
You can try this way:
int mPageEndOffset = 0;
int mPageLimit = 10;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
connectDetector = new ConnectDetector(getActivity());
if(connectDetector.getConnection()) {
alanRef = new Firebase(firebaseURL).child(EventChatActivity.SuperCateName + "/ " + eventDetail.getCategory_name() + "/ " + eventDetail.getEvent_title() + "/ " + EventChatActivity.eventID).child("StadiumChat");
alanQuery = alanRef.limitToFirst(mPageLimit);
userName = MyApp.preferences.getString(MyApp.USER_NAME, null);
EventChatActivity.eventID).child("StadiumChat");
}
}
This will fetch records of latest 10 messages from firebase.