我正在开发一个应用程序,我想从我的firebase数据库中读取数据,并将其放在我的listView中。
我想要有3种不同的读取操作。
首次启动应用时,我想要读取最近的10行。
当我到达listView的末尾时,我想再获得10行,从目前为止已读过的最旧行开始。
当我拉到listView的顶部时,我想获得在现在显示为第一行的行之后创建的所有行。 (这可以理解......?)
注意:我已经在读取listview的顶部和末尾时实现了侦听器,因此它只是我需要的firebase调用。
应该怎么做?
答案 0 :(得分:8)
我已经解决了这个问题,这就是我所做的。
<强> 1。当应用程序首次启动时,我想要读取最近的10行。
//read the 15 latest posts from the db
postQuery.limit(15).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Read each child of the user
for(DataSnapshot child : dataSnapshot.getChildren())
{
//If this is the first row we read, then this will be the oldest post that is going to be read,
//Therefore we save the name of this post for further readings.
if(prevOldestPostId.equals(oldestPostId))
{
//Save the name of the last read post, for later readings
oldestPostId = child.getName();
}
//This if-statment is just a fail-safe, since I have an counter in the post, and that one I do not want to read.
if(child.getName() != "nrOfPosts")
{
//Gather the data from the database
UserPost readPost = new UserPost();
for(DataSnapshot childLvl2 : child.getChildren()) {
if(childLvl2.getName() == "post")
readPost.setMessage(childLvl2.getValue().toString());
else
readPost.setDate(childLvl2.getValue().toString());
}
//Add the data to a temporary List
toAdd.add(0, readPost);
}
//Keep the name of the newest post that has been read, we save this for further readings.
newestPostId = child.getName();
}
//prevOlddestPostId is helping us to keep the the currently oldest post-name.
prevOldestPostId = oldestPostId;
//Add the new posts from the database to the List that is connected to an adapter and later on a ListView.
for (UserPost aToAdd : toAdd) thePosts.add(aToAdd);
// We need notify the adapter that the data have been changed
mAdapter.notifyDataSetChanged();
// Call onLoadMoreComplete when the LoadMore task, has finished
mListView.onRefreshComplete();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
<强> 2。当我到达listView的末尾时,我希望再获得10行,从目前为止已读过的最旧行开始。
postQuery.endAt(1, oldestPostId).limit(15).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Gather the data
for(DataSnapshot child : dataSnapshot.getChildren())
{
if(prevOldestPostId.equals(oldestPostId) && !child.getName().equals("nrOfPosts"))
{
//Save the name of the last read post, for later readings
oldestPostId = child.getName();
}
if(child.getName() != "nrOfPosts" && !prevOldestPostId.equals(child.getName()))
{
UserPost readPost = new UserPost();
for(DataSnapshot childLvl2 : child.getChildren()) {
if(childLvl2.getName() == "post")
readPost.setMessage(childLvl2.getValue().toString());
else
readPost.setDate(childLvl2.getValue().toString());
}
toAdd.add(0, readPost);
}
}
prevOldestPostId = oldestPostId;
//Insert it to the List for the listView
for (UserPost aToAdd : toAdd)
{
thePosts.add(aToAdd);
// We need notify the adapter that the data have been changed
mAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
第3。当我拉到listView的顶部时,我想获得在现在显示为第一行的行之后创建的所有行。 (这可以理解......?)
postQuery.startAt(1, newestPostId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot child : dataSnapshot.getChildren())
{
if(child.getName() != "nrOfPosts")
{
UserPost readPost = new UserPost();
for(DataSnapshot childLvl2 : child.getChildren()) {
if(childLvl2.getName() == "post")
readPost.setMessage(childLvl2.getValue().toString());
else
readPost.setDate(childLvl2.getValue().toString());
}
//Prevent from get the currently newest post again...
if(!readPost.getMessage().equals(thePosts.get(0).getMessage()))
toAdd.add(readPost);
//Save the name of the last read post, for later readings
newestPostId = child.getName();
}
}
for (UserPost aToAdd : toAdd)
{
thePosts.add(0, aToAdd);
// Call onLoadMoreComplete when the LoadMore task, has finished
mListView.onRefreshComplete();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
我希望我能帮助至少一个人。