从Firebase读取数据,并在ListView中填充 [英] Read data from firebase, and populate in listview

查看:276
本文介绍了从Firebase读取数据,并在ListView中填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我想从我的firebase数据库读取数据,并将其放入我的listView中。



有3种不同的读取操作I希望拥有。


  1. 当应用程序首次启动时,我想要读取10个最近的行。


  2. 当我到达listView的末尾时,我想再获得10行,从迄今为止读取的最老的一行开始。


  3. 当我拉到listView的顶部时,我想要获得所有现在显示为第一个行的行。 (这是可以理解的...?)

    $ b

    注意:我已经实现了侦听器因为当我阅读listview的顶部和末尾时,所以它只是我需要的firebase-calls。



    这应该怎么做?

    解决方案

    我已经解决了这个问题,这里是我所做的。强> 1。当应用程序第一次启动时,我想要读取最近10行。

      //读取(15).addListenerForSingleValueEvent(new ValueEventListener(){
    @Override
    public void onDataChange(DataSnapshot dataSnapshot){
    //读取每个(DataSnapshot child:dataSnapshot.getChildren())
    {
    //如果这是我们阅读的第一行,那么这将是最旧的帖子,被读取,
    //因此我们保存这篇文章的名字作进一步阅读
    if(prevOldestPostId.equals(oldestPostId))
    {
    //保存上次阅读的帖子,以后的阅读
    oldestPostId = child.getName();
    }
    //这个if-statment只是一个故障安全,因为我在帖子中有一个计数器,那一个我不想读。
    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());

    //将数据添加到临时列表
    toAdd.add(0,readPost);
    }
    //保留已读取的最新帖子的名称,我们将其保存以供进一步读取。
    newestPostId = child.getName();
    }
    // prevOlddestPostId正在帮助我们保留当前最古老的后期名称。
    prevOldestPostId = oldestPostId;
    //将新的帖子从数据库添加到连接到适配器的列表中,然后在ListView上添加。
    for(UserPost aToAdd:toAdd)thePosts.add(aToAdd);

    //我们需要通知适配器数据已被更改
    mAdapter.notifyDataSetChanged();

    //当LoadMore任务完成
    mListView.onRefreshComplete()时,调用onLoadMoreComplete;
    }

    @Override
    public void onCancelled(FirebaseError firebaseError){
    $ b}
    });

    2。当我到达listView的末尾时,我想再获得10行,从迄今为止读到的最老的行开始。

      postQuery.endAt(1,oldestPostId).limit(15).addListenerForSingleValueEvent(new ValueEventListener(){
    @Override
    public void onDataChange(DataSnapshot dataSnapshot){
    //收集数据
    for(DataSnapshot child:dataSnapshot.getChildren())
    {
    if(prevOldestPostId.equals(oldestPostId)&&!child.getName()。equals nrOfPosts))
    {
    //保存上次读取的文章的名字,用于以后的阅读
    oldestPostId = child.getName();
    }

    if(child.getName()!=nrOfPosts&&!prevOldestPostId.equals(child.getName()))
    {
    UserPost readPost = new UserPost(); $ b (DataSnapshot childLvl2:child.getChildren()){

    $ b if(childLvl2.getName()==post)
    readPost.setMessage(childLvl2.getValue()。toString());
    else
    readPost.setDate(childLvl2.getValue()。toString());
    }
    toAdd.add(0,readPost);

    }
    }
    prevOldestPostId = oldestPostId;

    //将它插入listView列表
    (UserPost aToAdd:toAdd)
    {
    thePosts.add(aToAdd);


    //我们需要通知适配器数据已被更改
    mAdapter.notifyDataSetChanged();


    $ b @Override
    public void onCancelled(FirebaseError firebaseError){


    });

    3。当我拉到列表视图的顶部,我想要获得所有行后,现在显示为第一个行。 (这是可以理解的...?)

      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();

    (DataSnapshot childLvl2:child.getChildren()){
    if (childLvl2.getName()==post)
    readPost.setMessage(childLvl2.getValue()。toString());
    else
    readPost.setDate(childLvl2.getValue()。 toString());
    }
    //防止再次获取当前最新的帖子...
    if(!readPost.getMessage()。equals(thePosts.get(0).getMessage ()))
    toAdd.add(re adPost);

    //保存上次阅读的文章的名称,以供以后阅读
    newestPostId = child.getName();


    (UserPost aToAdd:toAdd)
    {
    thePosts.add(0,aToAdd);

    //当LoadMore任务完成
    mListView.onRefreshComplete()时,调用onLoadMoreComplete;


    $ b @Override
    public void onCancelled(FirebaseError firebaseError){


    });

    我希望我可以帮助至少一个人。

    I am developing an app in which I want to read data from my firebase database, and put it in my listView.

    there are 3 different read-actions I want to have.

    1. When the app is first started, I want to get the 10 most recent rows read.

    2. When I reach the end of the listView, I want to get 10 more rows, starting at the oldest one that has been read so far.

    3. when I pull to the top of the listView, I want to get all the rows that has been made after the row that is now displayed as the first one. (Is that understandable...?)

    Note: I have already implemented listeners for when I read the top and the end of the listview, so it's only the firebase-calls that I need.

    How should this be done?

    解决方案

    I have solved the issue, and here is what I have done.

    1. When the app is first started, I want to get the 10 most recent rows read.

    //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. When I reach the end of the listView, I want to get 10 more rows, starting at the oldest one that has been read so far.

    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. when I pull to the top of the listView, I want to get all the rows that has been made after the row that is now displayed as the first one. (Is that understandable...?)

    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) {
    
        }
    });
    

    I hope I can be of help to someone atleast.

    这篇关于从Firebase读取数据,并在ListView中填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆