android - Firebase 查看 friend 的帖子

标签 android firebase firebase-realtime-database

我想填充用户及其 friend 的所有帖子。附上我的数据库实现

用户实现 User implementation

帖子实现 Posts Implementation

文本 Json:-

{
  "UserTable" : {
    "KhusbuDOTaATgDOTcom" : "-KIcHQ66Vig1aUjmWIfg",
    "aDOTkATgDOTcom" : "-KIYwF0kWZ97pkm0od-F",
    "amATaDOTa" : "-KIa86JUxWbDnqStdDxh",
    "amanATaDOTa" : "-KIa9eEbHu4YNGnjpbZv",
    "arnabDOTdaATgmailDOTcom" : "-KIYu5eUE04if17exaYe",
    "imakshayboraATgmailDOTcom" : "-KIT9PnIJbOGigKT1BHa",
    "newuserATgmailDOTcom" : "-KJ12Z9IZNz_kGNIcPs1",
    "rameshDOTtATgmailDOTcom" : "-KIa0kfOzm_hlUJ_pBbV",
    "sovanDOTgATgmailDOTcom" : "-KIT9WWG--HaRzCm6fg_",
    "test1ATuserDOTcom" : "-KJEgt7DD125dBgwSPSh",
    "testATtDOTt" : "-KIa4-dHS7-iVT9b2Jh4"
  },
  "message" : "Do you have data? You'll love Firebase.",
  "message1" : "akshay is the best",
  "posts" : {
    "-KIa9UA-FUMSDg4uwVTf" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9760896 77.7576878",
      "post" : "hi",
      "timestamp" : 1464150389786
    },
    "-KIa9XhunDYdVzj0g8Dx" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9762825 77.7577278",
      "post" : "hello",
      "timestamp" : 1464150404299
    },
    "-KIcHFDcUuUu8nUO8Wg2" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9757344 77.7575097",
      "post" : "hi Khusbu ",
      "timestamp" : 1464185980168
    },
    "-KIi3chQa1WW_K3JL_O5" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.976163 77.7577681",
      "post" : "hi bolo",
      "timestamp" : 1464283075118
    },
    "-KIi7ab1al65UBp4IXDB" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "mLocation" : "12.9761519 77.7577668",
      "post" : "hi",
      "timestamp" : 1464284113805
    },
    "-KIi7dxmgIwbJnl2KnBP" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "mLocation" : "12.9761519 77.7577668",
      "post" : "hibn",
      "timestamp" : 1464284127520
    },
    "-KIi83HBrUaElIwj5bT1" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "mLocation" : "12.9761519 77.7577668",
      "post" : "aka ",
      "timestamp" : 1464284236238
    },
    "-KIi8PeO_T00FA3bRYm_" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "mLocation" : "12.9761519 77.7577668",
      "post" : "ji",
      "timestamp" : 1464284326992
    },
    "-KIiDwSd7H65RDilOZ00" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "mLocation" : "12.9760913 77.7577808",
      "post" : "hj",
      "timestamp" : 1464285776169
    },
    "-KImYPEKRS4DqwnOvi-W" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "mLocation" : "12.9761887 77.7576894",
      "post" : "hi",
      "timestamp" : 1464358249771
    },
    "-KImcjHn6vtkYmwRUiFF" : {
      "author" : "-KIa9eEbHu4YNGnjpbZv",
      "mLocation" : "12.9757941 77.7578351",
      "post" : "mat dikhana isko",
      "timestamp" : 1464359646738
    },
    "-KImhPxhaUcvs228_sQ6" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9761605 77.7577076",
      "post" : "yum hai hum",
      "timestamp" : 1464360874200
    },
    "-KImmbSE0ytEDxdrvt4K" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9756235 77.7576516",
      "post" : "goodnight ",
      "timestamp" : 1464362237046
    },
    "-KImrq-fVNYTZZ_7aWkG" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9761095 77.7577431",
      "post" : "test1",
      "timestamp" : 1464363606381
    },
    "-KImsBvI_v0ChkTfNr2H" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9761095 77.7577431",
      "post" : "test2😃",
      "timestamp" : 1464363700223
    },
    "-KIrKTvw8oSWfE07R6WM" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9761034 77.7577107",
      "post" : "test3",
      "timestamp" : 1464438485113
    },
    "-KIrKsN-2HRpwbRz_1nq" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9761034 77.7577107",
      "post" : "test4",
      "timestamp" : 1464438589263
    },
    "-KIrLHKJeuqxb4Wp48Qn" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9758123 77.7578919",
      "post" : "test5",
      "timestamp" : 1464438695998
    },
    "-KIrMwNcSBR4ykm2n5ks" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9760823 77.7576796",
      "post" : "test6",
      "timestamp" : 1464439130185
    },
    "-KIrNHQLY-PT07lEnFo1" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9760823 77.7576796",
      "post" : "test 7😉",
      "timestamp" : 1464439220275
    },
    "-KIrOKW0wkX4Jjtp_6lM" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9760568 77.7577028",
      "post" : "test8",
      "timestamp" : 1464439495086
    },
    "-KIrQE8PWxVP7dsW4LUQ" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9760223 77.7578286",
      "post" : "test9",
      "timestamp" : 1464439993419
    },
    "-KIrYsXtkfM6M2vClmCX" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.97617 77.7577447",
      "post" : "test 10",
      "timestamp" : 1464442260015
    },
    "-KIrdL0HJgyTLX6fDsjL" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9760616 77.7576665",
      "post" : "test11",
      "timestamp" : 1464443691516
    },
    "-KIv5c1OIISFBXXTeQF0" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "authorName" : "sovan",
      "mLocation" : "12.9759527 77.7578142",
      "post" : "hello #sovan",
      "timestamp" : 1464501699050
    },
    "-KIwsU1TyXRV7PTYqfhv" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "authorName" : "sovan",
      "mLocation" : "12.9755286 77.7579091",
      "post" : "test from sovan",
      "timestamp" : 1464531546607
    },
    "-KIwvMArad5wufME46d2" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "authorName" : "sovan",
      "mLocation" : "12.9762825 77.7577278",
      "post" : "test2 from sovan",
      "timestamp" : 1464532300871
    },
    "-KIx9jBV7qqOhb_04FiR" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "authorName" : "sovan",
      "mLocation" : "12.9761452 77.757783",
      "post" : "test3 from sovan",
      "timestamp" : 1464536331444
    },
    "-KIxATLiQRW6VPUulgLl" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "authorName" : "sovan",
      "mLocation" : "12.9762063 77.7577588",
      "post" : "test4 from sovan",
      "timestamp" : 1464536524673
    },
    "-KJ-aFZu75WE8fu4alTM" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9791569 77.7288152",
      "post" : "test12 from akshay",
      "timestamp" : 1464593877941
    },
    "-KJ132_Yz1lInWMM1bZc" : {
      "author" : "-KJ12Z9IZNz_kGNIcPs1",
      "authorName" : "newuser",
      "mLocation" : "12.9760395 77.7577474",
      "post" : "hum hai new user",
      "timestamp" : 1464618466004
    },
    "-KJ4SJGPOeh3OxkcVuZC" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9812033 77.7240957",
      "post" : "hi test 13",
      "timestamp" : 1464675421809
    },
    "-KJ6DbioHox2NSLR0Z8T" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9759283 77.7577924",
      "post" : "feeling #great #partying # ",
      "timestamp" : 1464705121602
    },
    "-KJ6EAoFUoGPOyduvkvf" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9759283 77.7577924",
      "post" : "#coding",
      "timestamp" : 1464705269345
    },
    "-KJ6J4ehY86MbFdL6mN-" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "hashTags" : [ "#you" ],
      "mLocation" : "12.9757097 77.7578994",
      "post" : "love making ##you",
      "timestamp" : 1464706555095
    },
    "-KJ6JJeQ3VZ-6B59Z7xx" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "hashTags" : [ null, "#a", "#b", "#c", "#d" ],
      "mLocation" : "12.9757097 77.7578994",
      "post" : "#a# b #b #c #d",
      "timestamp" : 1464706616439
    },
    "-KJ6Jqy4Q-DxJv9HCaaO" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "hashTags" : [ "#test1" ],
      "mLocation" : "12.9758581 77.7577817",
      "post" : "#test1",
      "timestamp" : 1464706756825
    },
    "-KJ6JxRZ5qczbKm-e_a2" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "hashTags" : [ "#test2", "#test3" ],
      "mLocation" : "12.9758581 77.7577817",
      "post" : "#test2 #test3",
      "timestamp" : 1464706783346
    }
  },
  "user" : {
    "-KIT9PnIJbOGigKT1BHa" : {
      "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9af3f7fbf1e9f2fbe3f8f5e8fbdafdf7fbf3f6b4f9f5f7" rel="noreferrer noopener nofollow">[email protected]</a>",
      "friends" : {
        "-KIl0na9cZ1EY9jF4HSi" : "-KIa0kfOzm_hlUJ_pBbV",
        "-KIl18efwTfR1sq-6unv" : "-KIcHQ66Vig1aUjmWIfg",
        "-KIlN2cA8nSMU-0ljDgr" : "-KIT9WWG--HaRzCm6fg_",
        "-KIlOczOGj9oWt3itAoH" : "-KIa4-dHS7-iVT9b2Jh4",
        "-KIlOzMIiMIdFEfqdRWF" : "-KIYwF0kWZ97pkm0od-F",
        "-KIlPas-62dWJSGaWVd7" : "-KIa86JUxWbDnqStdDxh",
        "-KImcszKNBWW1O2BhZg6" : "-KIa9eEbHu4YNGnjpbZv"
      },
      "name" : "akshay",
      "password" : "alcohol",
      "uniqueId" : "-KIT9PnIJbOGigKT1BHa"
    },
    "-KIT9WWG--HaRzCm6fg_" : {
      "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="02716d74636c2c6542656f636b6e2c616d6f" rel="noreferrer noopener nofollow">[email protected]</a>",
      "friends" : {
        "-KIlN2eNvL2poRh2L1aG" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "sovan",
      "password" : "sovan",
      "uniqueId" : "-KIT9WWG--HaRzCm6fg_"
    },
    "-KIYu5eUE04if17exaYe" : {
      "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f594879b9497db9194b59298949c99db969a98" rel="noreferrer noopener nofollow">[email protected]</a>",
      "name" : "arnab",
      "password" : "arnab",
      "uniqueId" : "-KIYu5eUE04if17exaYe"
    },
    "-KIYwF0kWZ97pkm0od-F" : {
      "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8deca3e6cdeaa3eee2e0" rel="noreferrer noopener nofollow">[email protected]</a>",
      "friends" : {
        "-KIlOzMIiMIdFEfqdRWG" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "ardhendu ",
      "password" : "ak",
      "uniqueId" : "-KIYwF0kWZ97pkm0od-F"
    },
    "-KIa0kfOzm_hlUJ_pBbV" : {
      "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3745565a52445f194377505a565e5b1954585a" rel="noreferrer noopener nofollow">[email protected]</a>",
      "friends" : {
        "-KIl0naLO88NGJZsIWzu" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "ramesh ",
      "password" : "ramesh",
      "uniqueId" : "-KIa0kfOzm_hlUJ_pBbV"
    },
    "-KIa4-dHS7-iVT9b2Jh4" : {
      "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="097d6c7a7d497d277d" rel="noreferrer noopener nofollow">[email protected]</a>",
      "friends" : {
        "-KIlOczeUi8zciZkvtSC" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "test",
      "password" : "test",
      "uniqueId" : "-KIa4-dHS7-iVT9b2Jh4"
    },
    "-KIa86JUxWbDnqStdDxh" : {
      "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83e2eec3e2ade2" rel="noreferrer noopener nofollow">[email protected]</a>",
      "friends" : {
        "-KIlPas9zzXvWLXdJva6" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "am",
      "password" : "am",
      "uniqueId" : "-KIa86JUxWbDnqStdDxh"
    },
    "-KIa9eEbHu4YNGnjpbZv" : {
      "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0a1ada1ae80a1eea1" rel="noreferrer noopener nofollow">[email protected]</a>",
      "friends" : {
        "-KImcszKNBWW1O2BhZg7" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "aman",
      "password" : "aman",
      "uniqueId" : "-KIa9eEbHu4YNGnjpbZv"
    },
    "-KIcHQ66Vig1aUjmWIfg" : {
      "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ad1f2efe9f8efb4fbdafdb4f9f5f7" rel="noreferrer noopener nofollow">[email protected]</a>",
      "friends" : {
        "-KIl18egKD9k1xv8rhd5" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "Khusbu Agarwal ",
      "password" : "Khusbu",
      "uniqueId" : "-KIcHQ66Vig1aUjmWIfg"
    },
    "-KJ12Z9IZNz_kGNIcPs1" : {
      "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4cac1d3d1d7c1d6e4c3c9c5cdc88ac7cbc9" rel="noreferrer noopener nofollow">[email protected]</a>",
      "name" : "newuser",
      "password" : "newuser",
      "uniqueId" : "-KJ12Z9IZNz_kGNIcPs1"
    },
    "-KJEgt7DD125dBgwSPSh" : {
      "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f4b5a4c4b0e7f4a4c5a4d115c5052" rel="noreferrer noopener nofollow">[email protected]</a>",
      "name" : "testuser",
      "password" : "test1",
      "uniqueId" : "-KJEgt7DD125dBgwSPSh"
    }
  }
}

目前我通过下面的代码实现了这一点:

    postQuery=posts.orderByChild("timestamp");

    friends.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot friendSnap) {
            for(DataSnapshot snap:friendSnap.getChildren()) {
                friendStringList.add(snap.getValue(String.class));
            }
            postQuery.addValueEventListener(new ValueEventListener() {

                @Override
                public void onDataChange(DataSnapshot postSnap) {
                    postsTest.clear();
                    for (DataSnapshot snapshot : postSnap.getChildren()) {
                        if (friendStringList.contains(snapshot.getValue(PostPOJO.class).getAuthor()) || (snapshot.getValue(PostPOJO.class).getAuthor().equals(uniqueId))) {
                                postsTest.add(snapshot.getValue(PostPOJO.class));
                        }
                    }
                    Collections.reverse(postsTest);
                    hide();
                    postAdapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

这是一个很好的实现方法吗?或者我可以使用一些查询来以更好的方式获得东西吗?

请指导。

最佳答案

我现在就是这样实现上面的。如果有人有更好的实现方法,请发帖。首先检查 20 个帖子,然后滚动用户获取下一个数据集。

postQuery=posts.orderByKey().startAt("").limitToLast(20);

        friends.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot friendSnap) {
                for(DataSnapshot snap:friendSnap.getChildren()) {
                    friendStringList.add(snap.getValue(String.class));
                }
                postQuery.addValueEventListener(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot postSnap) {
                        postsTest.clear();
                        int count=0;
                        for (DataSnapshot snapshot : postSnap.getChildren()) {
                            if(count==0) {
                                start = snapshot.getKey();
                                count++;
                            }
                           // start=snapshot.getKey();
                            if (friendStringList.contains(snapshot.getValue(PostPOJO.class).getAuthor()) || (snapshot.getValue(PostPOJO.class).getAuthor().equals(uniqueId))) {
                                    postsTest.add(snapshot.getValue(PostPOJO.class));
                            }
                        }
                        Collections.reverse(postsTest);
                        hide();
                        count=0;
                        postAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
            {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy)
                {
                    if(dy > 0) //check for scroll down
                    {
                        visibleItemCount = mLayoutManager.getChildCount();
                        totalItemCount = mLayoutManager.getItemCount();
                        pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                        if (loading)
                        {
                            if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount)
                            {
                                loading = false;
                                Log.v("...", "Last Item Wow !");

                                //Do pagination.. i.e. fetch new data
                                show();
                                postQuery=posts.orderByKey().endAt(start).limitToLast(20);

                                postQuery.addValueEventListener(new ValueEventListener() {

                                    @Override
                                    public void onDataChange(DataSnapshot postSnap) {
                                        postsList.clear();
                                        int count=0;
                                        for (DataSnapshot snapshot : postSnap.getChildren()) {
                                            if(count==0) {
                                                start = snapshot.getKey();
                                                count++;
                                            }
                                            //start=snapshot.getKey();
                                            if (friendStringList.contains(snapshot.getValue(PostPOJO.class).getAuthor()) || (snapshot.getValue(PostPOJO.class).getAuthor().equals(uniqueId))) {
                                                postsList.add(snapshot.getValue(PostPOJO.class));
                                                loading=true;

                                            }
                                            else if(snapshot.getKey().equals(FirstPostKey)){
                                                loading=false;
                                                Snackbar.make(mView,"Reached End",Snackbar.LENGTH_LONG).show();
                                            }
                                        }
                                        postsList.remove(postsList.size()-1);
                                        Collections.reverse(postsList);
                                        postsTest.addAll(postsList);
                                        hide();
                                        count=0;
                                        postAdapter.notifyDataSetChanged();
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });


                            }
                        }
                    }
                }
            });

关于android - Firebase 查看 friend 的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37600353/

相关文章:

android - 无需注册 Firebase 数据库即可手动添加用户

android - 无法将 APK 安装到设备

android - 如果身份验证不成功,Firebase Auth 是否会返回错误代码?

android - Google Play 私有(private) channel "Restrict Distribution"不可见

Firebase:基本帐户/用户角色(仅限通知)

Firebase 规则自行重置

ios - 收到此错误 'text' 的使用不明确

ios - 当应用程序从 Firebase 实时数据库接收数据时如何显示 UIActivityIndi​​cator

java - 在 3 点之间画一个正方形

android - 连接 VPN 时缺少 ConnectivityManager