从 firebase 读取数据,并在 listview 中填充
Posted
技术标签:
【中文标题】从 firebase 读取数据,并在 listview 中填充【英文标题】:Read data from firebase, and populate in listview 【发布时间】:2014-12-10 18:58:19 【问题描述】:我正在开发一个应用程序,我想在其中从我的 firebase 数据库中读取数据,并将其放入我的 listView 中。
我想要 3 种不同的阅读操作。
第一次启动应用时,我想读取最近的 10 行。
当我到达 listView 的末尾时,我想再获得 10 行,从目前已读取的最旧的行开始。
当我拉到 listView 的顶部时,我想获取现在显示为第一行的行之后的所有行。 (这可以理解吗...?)
注意:当我阅读列表视图的顶部和末尾时,我已经实现了侦听器,所以我只需要 firebase-calls。
这应该怎么做?
【问题讨论】:
您已经尝试过什么了吗?如果是这样,请分享代码。如果没有:firebase.com/docs/android/guide/retrieving-data.html 我刚刚得到它想要的工作,我会在这里发布答案。 我觉得这很有用qiita.com/niusounds/items/2de6e08d308f95b8f4dd 【参考方案1】:我已经解决了这个问题,这就是我所做的。
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)
);
我希望我至少可以对某人有所帮助。
【讨论】:
你能解释一下你代码中的 postQuery 是什么吗?\以上是关于从 firebase 读取数据,并在 listview 中填充的主要内容,如果未能解决你的问题,请参考以下文章
从 Firebase 检索数据并在另一个 Firebase 数据库参考中使用它
读取 longlat 数据实时 Firebase 和实现以映射 Android Studio
使用 Firebase 从不同的集合中获取数据,并在数据更改时更新 UI