本文介绍了Firebase看朋友帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想填充用户和他们的朋友的所有帖子。我的数据库实现已经附上

用户执行



文章执行








$ bUserTable:
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你有数据? ,
message1:akshay is 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,
时间戳:1464150404299
},
-KIcHFDcUuUu8nUO8Wg2:{
作家: -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:{
作者:-KI T9WWG - 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,
时间戳:1464285776169
},
-KI mYPEKRS4DqwnOvi-W:
author:-KIT9WWG - HaRzCm6fg_,
mLocation:12.9761887 77.7576894,
post:hi,
时间戳:1464358249771
},
-KImcjHn6vtkYmwRUiFF:{
作家: -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,
po st:test1,
timestamp:1464363606381
},
-KImsBvI_v0ChkTfNr2H:{
author:-KIT9PnIJbOGigKT1BHa,
mLocation:12.9761095 77.7577431,
post:test2ðŸ〜f,
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
时间戳:1464438589263
},
-KIrLHKJeuqxb4Wp48Qn:{
作家: -KIT9PnIJbOGigKT1BHa,
mLocation: 12.9758123 77.7578919 ,
post:test5,
timestamp:1464438695998
},
-KIrMwNcSBR4ykm2n5ks:{
author:-KIT9PnIJbOGigKT1BH a,
authorName:akshay,
mLocation:12.9760823 77.7576796,
post:test6,$ b $timestamp:1464439130185
},
-KIrNHQLY-PT07lEnFo1:{
author:-KIT9PnIJbOGigKT1BHa,
authorName:akshay,
mLocation: 12.9760823 77.7576796
post:test7ðŸ〜‰,
timestamp:1464439220275
},
-KIrOKW0wkX4Jjtp_6lM:{
作者:-KIT9PnIJbOGigKT1BHa,
authorName:akshay,
mLocation:12.9760568 77.7577028,
post:test8,
时间戳:1464439495086
},
-KIrQE8PWxVP7dsW4LUQ:{
author:-KIT9PnIJbOGigKT1BHa,
authorName:akshay,
mLocation:12.9760223 77.7578286,
post:test9,
timestamp:1464439993419
},
-KIrYsXtkfM6M2vClmCX:{
作者:-KIT9PnIJbOGigKT1 BHa,
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:sovan测试,
timestamp:1464531546607
},
-KIwvMArad5wufME46d2: {
作者:-KIT9WWG-
post:test2 from sovan,
timestamp,
authorName:sovan,
mLocation:12.9762825 77.7577278 :

-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:哼哼新用户,
timestamp:1464618466004
},
-KJ4SJGPOeh3OxkcVuZC:{
author:-KIT9PnIJbOGigKT1BHa,
authorName: akshay,
mLocation:12.9812033 77.7240957,
post:hi test 13,
timestamp:1464675421809
},
-KJ6DbioHox2NSLR0Z8T:{
作家: -KIT9PnIJbOGigKT1BHa,
AUTHORNAME: 阿克沙伊,
mLocation: 12.9759283 77.7577924,
后:感觉#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,
authorNam e:akshay,
hashTags:[#test2,#test3],
mLocation:12.9758581 77.7577817,
post: test2#test3,
timestamp:1464706783346
}
},
user:{
-KIT9PnIJbOGigKT1BHa:{
email : [email protected]
朋友:{
-KIl0na9cZ1EY9jF4HSi: -KIa0kfOzm_hlUJ_pBbV,
-KIl18efwTfR1sq-6unv: -KIcHQ66Vig1aUjmWIfg,
-KIlN2cA8nSMU-0ljDgr:-KIT9WWG -HaRzCm6fg_,
-KIlOczOGj9oWt3itAoH:-KIa4-dHS7-iVT9b2Jh4,
-KIlOzMIiMIdFEfqdRWF:-KIYwF0kWZ97pkm0od-F ,
-KIlPas-62dWJSGaWVd7: -KIa86JUxWbDnqStdDxh,
-KImcszKNBWW1O2BhZg6: -KIa9eEbHu4YNGnjpbZv
},
名: 阿克沙伊,
password:alcohol,
uniqueId:-KIT9PnIJbOGigKT1BHa
},
-KIT9WWG - HaRzCm6fg_:{
email: [email protected]
星期五结尾:
-KIlN2eNvL2poRh2L1aG:-KIT9PnIJbOGigKT1BHa
},
name:sovan,
password:sovan,
uniqueId:-KIT9WWG - HaRzCm6fg_
},
-KIYu5eUE04if17exaYe:{
email:[email protected]
name:arnab,
password:arnab,
uniqueId:-KIYu5eUE04if17exaYe
},
-KIYwF0kWZ97pkm0od-F:{
email:[email protected]
friends:{
-KIlOzMIiMIdFEfqdRWG:-KIT9PnIJbOGigKT1BHa
},
name :ardhendu,
password:ak,
uniqueId:-KIYwF0kWZ97pkm0od-F
},
-KIa0kfOzm_hlUJ_pBbV:{
email:[email protected]
friends:{
-KIl0LLO88NGJZsIWzu:-KIT9PnIJbOGigKT1BHa
},
name :ramesh,
password:ramesh,
uniqueId:-KIa0kfOzm_hlUJ_pBbV
},
-KIa4-dHS7-iVT9b2Jh4:{
email:test @ tt,
friends:{
-KIlOczeUi8zciZkvtSC:-KIT9PnIJbOGigKT1BHa
},
name:test,
password:test,
uniqueId:-KIa4-dHS7-iVT9b2Jh4
},
-KIa86JUxWbDnqStdDxh:{
email:am @ aa,
friends:{
-KIlPas9zzXvWLXdJva6:-KIT9PnIJbOGigKT1BHa
$ bname:am,
password:am,
uniqueId:-KIa86JUxWbDnqStdDxh
},
-KIa9eEbHu4YNGnjpbZv:{
email:aman @ aa,
friends:{
-KImcszKNBWW1O2BhZg7:-KIT9PnIJbOGigKT1BHa
},
name:aman,
password:aman,
uniqueId:-KIa9eEbHu4YNGnjpbZv
},
-KIcHQ66Vig1aUjmWIfg:{
email:[email protected]
friends:{
-KIl18egKD9k1xv8rhd5:-K IT9PnIJbOGigKT1BHa

name:Khusbu Agarwal,
password:Khusbu,
uniqueId:-KIcHQ66Vig1aUjmWIfg
} ,
-KJ12Z9IZNz_kGNIcPs1:{
email:[email protected]
name:newuser,
password:newuser ,
uniqueId:-KJ12Z9IZNz_kGNIcPs1
},
-KJEgt7DD125dBgwSPSh:{
email:[email protected]
name:testuser,
password:test1,
uniqueId:-KJEgt7DD125dBgwSPSh
}
}
}

目前我正在通过以下代码实现此目的:

  postQuery = posts.orderByChild(timestamp); (DataSnapshot snap:friendSnap.getChildren())

friends.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot friendSnap){
{
friendStringList.add(snap.getValue(String.class));
}
postQuery.addValueEventListener(new ValueEventListener(){
$ b @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(postsTe ST);
hide();
postAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(DatabaseError databaseError){
$ b}
});



$ @覆盖
public void onCancelled(DatabaseError databaseError){

}
}) ;

这是一个很好的方法来实现还是我可以使用一些查询来获得更好的方法?



请指南。

解决方案

这条路。如果有人有更好的方式来实施请张贴。首先检查20个帖子,然后从获取下一个数据集的用户滚动。

  postQuery = posts.orderByKey()。startAt()。limitToLast(20); (DataSnapshot snap:friendSnap.getChildren())

friends.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot friendSnap){
{
friendStringList.add(snap.getValue(String.class));
}
postQuery.addValueEventListener(new ValueEventListener(){
$ b @Override $ b $ (DataSnapshot snapshot:postSnap.getChildren()){
if(())$ public void onDataChange(DataSnapshot postSnap){
postsTest.clear();
int count = 0;
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){

$ b});



$ @覆盖
public void onCancelled(DatabaseError databaseError){

}
}) ;


mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrolled(RecyclerView recyclerView,int dx,int dy)
{
if(dy> 0)//检查向下滚动
{
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount() ;
如果((visibleItemCount + pastVisiblesItems)> = totalItemCount)
如果(加载)
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

{
loading = false;
Log.v(...,Last Item Wow!);

//分页。 ch新数据
show();
postQuery = posts.orderByKey()。endAt(start).limitToLast(20);

postQuery.addValueEventListener(new ValueEventListener(){
$ b @Override
public void onDataChange(DataSnapshot postSnap){
postsList.clear();
int count = 0;
(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()。等于(UNIQUEID))) {
postsList.add(snapshot.getValue(PostPOJO.class));
loading = true;

$ b 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){

$ b});


}
}
}
}
});


I want to populate all posts of user and their friends. My Database implementation is attached

User Implementation

Posts Implementation

Text 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" : "[email protected]",
      "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" : "[email protected]",
      "friends" : {
        "-KIlN2eNvL2poRh2L1aG" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "sovan",
      "password" : "sovan",
      "uniqueId" : "-KIT9WWG--HaRzCm6fg_"
    },
    "-KIYu5eUE04if17exaYe" : {
      "email" : "[email protected]",
      "name" : "arnab",
      "password" : "arnab",
      "uniqueId" : "-KIYu5eUE04if17exaYe"
    },
    "-KIYwF0kWZ97pkm0od-F" : {
      "email" : "[email protected]",
      "friends" : {
        "-KIlOzMIiMIdFEfqdRWG" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "ardhendu ",
      "password" : "ak",
      "uniqueId" : "-KIYwF0kWZ97pkm0od-F"
    },
    "-KIa0kfOzm_hlUJ_pBbV" : {
      "email" : "[email protected]",
      "friends" : {
        "-KIl0naLO88NGJZsIWzu" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "ramesh ",
      "password" : "ramesh",
      "uniqueId" : "-KIa0kfOzm_hlUJ_pBbV"
    },
    "-KIa4-dHS7-iVT9b2Jh4" : {
      "email" : "[email protected]",
      "friends" : {
        "-KIlOczeUi8zciZkvtSC" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "test",
      "password" : "test",
      "uniqueId" : "-KIa4-dHS7-iVT9b2Jh4"
    },
    "-KIa86JUxWbDnqStdDxh" : {
      "email" : "[email protected]",
      "friends" : {
        "-KIlPas9zzXvWLXdJva6" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "am",
      "password" : "am",
      "uniqueId" : "-KIa86JUxWbDnqStdDxh"
    },
    "-KIa9eEbHu4YNGnjpbZv" : {
      "email" : "[email protected]",
      "friends" : {
        "-KImcszKNBWW1O2BhZg7" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "aman",
      "password" : "aman",
      "uniqueId" : "-KIa9eEbHu4YNGnjpbZv"
    },
    "-KIcHQ66Vig1aUjmWIfg" : {
      "email" : "[email protected]",
      "friends" : {
        "-KIl18egKD9k1xv8rhd5" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "Khusbu Agarwal ",
      "password" : "Khusbu",
      "uniqueId" : "-KIcHQ66Vig1aUjmWIfg"
    },
    "-KJ12Z9IZNz_kGNIcPs1" : {
      "email" : "[email protected]",
      "name" : "newuser",
      "password" : "newuser",
      "uniqueId" : "-KJ12Z9IZNz_kGNIcPs1"
    },
    "-KJEgt7DD125dBgwSPSh" : {
      "email" : "[email protected]",
      "name" : "testuser",
      "password" : "test1",
      "uniqueId" : "-KJEgt7DD125dBgwSPSh"
    }
  }
}

Currently i am achieving this by the code below:

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

        }
    });

Is it a good way to implement or can i use some queries to get things in a better way?

Please Guide.

解决方案

I am now implementing the above in this way. If anyone is having a better way to implement please post. first checking 20 posts and then on scroll from the user fetching next data set.

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

                                    }
                                });


                            }
                        }
                    }
                }
            });

这篇关于Firebase看朋友帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 05:41