我正在使用FirebaseRecyclerAdapter,我需要显示使用ID等于true的帖子,这里是我的firebase数据库:
{
"Following" : {
"ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : {
"z2SNUlLd6mQM8nnlkU2VUws5Ggl2" : {
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2"
}
},
"pypD1SYZkbcYesk09WuMUY1AkTf1" : {
"ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : {
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1",
"Username" : "Ali"
},
"z2SNUlLd6mQM8nnlkU2VUws5Ggl2" : {
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "Ali"
}
},
"z2SNUlLd6mQM8nnlkU2VUws5Ggl2" : {
"ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : true,
"pypD1SYZkbcYesk09WuMUY1AkTf1" : true
}
},
"Posts" : {
"-KduqnVVczZf5uibQiZ-" : {
"Describe" : "gg",
"Profile" : "https://twitter.com/2Top/profile_image?size=original",
"Time" : 14881230655,
"UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username" : "Mike",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped906285501.jpg?alt=media&token=0c1a3a3d-6e48-4c4e-ba59-f5646bf8965f"
},
"-Ke5gJ00CxbjhOuhzLIx" : {
"Describe" : "hajj",
"Time" : 1488318465,
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "Mike",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1717103943.jpg?alt=media&token=a85b2488-5ac7-49a7-9ad0-dbf4e6f29389"
},
"-KeCuiFmUCpN19zwsTsR" : {
"Describe" : "a",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488439652,
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1",
"Username" : "2top",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1913886685.jpg?alt=media&token=c8ad26b2-8f09-453b-b48a-aad9e4d8b5c3"
},
"-KeD2fBUQ09HVMrvAneb" : {
"Describe" : "ee",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488441999,
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1",
"Username" : "2to7p",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1904338270.jpg?alt=media&token=d200b7fc-15e7-4dc4-9913-59c377929e9e"
},
"-KeDfnMWiB7k_4J3FBgZ" : {
"Describe" : "f12g",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488452517,
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "2top",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1872832638.jpg?alt=media&token=80239844-a872-47e2-9689-92d368dd9452"
},
"-KeDfrw4inUQFtIDXJHp" : {
"Describe" : "fg",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488452536,
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "2top",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1872832638.jpg?alt=media&token=80239844-a872-47e2-9689-92d368dd9452"
}
},
"Users" : {
"1DUK1pHEEoaImX8iOOspEMEWCPm2" : {
"Username" : "Tom"
},
"BQOwNdGAeRXiBnVj7ijY1EkFiDC3" : {
"Username" : "7uthaifah"
},
"ECWHIksxJ0Q5SUlIrev4BjnjmrJ3" : {
"Profile" : "https://lh3.googleusercontent.com/-p1mB5WbUN2s/AAAAAAAAAAI/AAAAAAAAAAo/r3Z4o-VSODQ/s200-c/photo.jpg",
"Username" : "Jice"
},
"I5o3JMpYHwZXQD0t2ogWHCQbrZv1" : {
"Profile" : "https://lh3.googleusercontent.com/-p1mB5WbUN2s/AAAAAAAAAAI/AAAAAAAAAAo/r3Z4o-VSODQ/s200-c/photo.jpg",
"Username" : "Mike"
},
"ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : {
"Cover" : "http://lucascontabil.com/home/wp-content/uploads/2015/05/city-wallpaper-7-400x200.jpg",
"Profile" : "https://lh5.googleusercontent.com/--IAMp28UlmE/AAAAAAAAAAI/AAAAAAAAAAA/AAomvV2EM8Ut2nW9j0YE2vRZhJyQBHMsrA/s200-c/photo.jpg",
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1",
"Username" : "Dom"
},
"ZxqBgLq4ekUJii05AvV86z0i3mH2" : {
"Profile" : "https://lh3.googleusercontent.com/-p1mB5WbUN2s/AAAAAAAAAAI/AAAAAAAAAAo/r3Z4o-VSODQ/s200-c/photo.jpg",
"Username" : "Jone"
},
"mZ5jjPNLlcc8OguVzWJLAYhavmg1" : {
"Profile" : "https://twitter.com/2Top/profile_image?size=original",
"Username" : "Twitter"
},
"pypD1SYZkbcYesk09WuMUY1AkTf1" : {
"Profile" : "https://lh3.googleusercontent.com/-p1mB5WbUN2s/AAAAAAAAAAI/AAAAAAAAAAo/r3Z4o-VSODQ/s200-c/photo.jpg",
"Username" : "Mike"
},
"z2SNUlLd6mQM8nnlkU2VUws5Ggl2" : {
"Cover" : "http://lucascontabil.com/home/wp-content/uploads/2015/05/city-wallpaper-7-400x200.jpg",
"Email" : "@dd",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "2277top"
}
}
}

我使用过这种方法,但它不起作用:
mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
mAuth = FirebaseAuth.getInstance();
mCurrentUser = mAuth.getCurrentUser();
mDatabaseFriends = FirebaseDatabase.getInstance().getReference().child("Following").child(mCurrentUser.getUid());
Query q = mDatabaseFriends.orderByKey().equalTo(true);
mQuery = mDatabase.orderByChild("UID").equalTo(q.toString());
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder>(
Getting_Posts.class, R.layout.post_card_design, PostViewHolder.class, mQuery) {
@Override
protected void populateViewHolder(final PostViewHolder viewHolder, final Getting_Posts model, int position) {}};
如果ids = true
,我需要使用它来显示帖子的查询这是我使用它的方法,但它只显示以下第一个用户:
mDatabaseFriends.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot following : dataSnapshot.getChildren()) {
Query X = mDatabase.orderByChild("UID").equalTo(following.getKey());
FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder>(
Getting_Posts.class, R.layout.post_card_design, PostViewHolder.class, X ) {}});
答案 0 :(得分:1)
您可以使用常规的RecyclerAdapter实现您想要的效果。使用您当前的数据库设计,您将无法将所需内容转换为单个firebase查询,因此FirebaseRecyclerAdapter已经用完。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
另一个选择是简单地更改数据库设计以允许使用FirebaseRecyclerAdapter或FirebaseIndexRecyclerAdapter(更有可能)。我认为你需要使用客户端扇出来实现这一点。
这些链接应该与您要实现的目标相关。
The Firebase Blog: Client-side fan-out
已经有一段时间了,我现在无法测试代码,但请告诉我这是否适合您。
首先是适配器
mDatabaseFriends.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot following : dataSnapshot.getChildren()) {
mDatabase.orderByChild("UID").equalTo(following.getKey()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Getting_Posts model = dataSnapshot.getValue(Getting_Posts.class);
// Here you can add your 'post' to a dataset that you can use to initialize a new RecyclerAdapter
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
以下列方式使用它。 在您的activity / fragment中声明以下属性。
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.PostViewHolder> {
private ArrayList<Getting_Posts> mdataSet;
public RecyclerAdapter(ArrayList<Getting_Posts> dataSet) {
mdataSet = dataSet;
}
@Override
public RecyclerAdapter.PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflatedView = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_card_design, parent, false);
return new PostViewHolder(inflatedView);
}
@Override
public void onBindViewHolder(RecyclerAdapter.PostViewHolder holder, int position) {
Gettings_Posts post = mdataSet.get(position);
// Set the properties of the holder
// I don't know what properties you used in the viewholder so I'll just give an example
// example: holder.Name.setText(post.getName());
}
@Override
public int getItemCount() {
return mdataSet.size();
}
public void add(int position, Getting_Posts post) {
mdataset.add(position, post);
notifyItemInserted(position);
}
}
在onCreate中将它们初始化为这样。
ArrayList<Getting_Posts> posts;
private RecyclerAdapter mAdapter;
private RecyclerView mRecycler;
然后添加原始解决方案,只需替换以下行
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); // Replace this with the ID of your recyclerView
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new RecyclerAdapter(posts);
mRecycler.setAdapter(mAdapter);
用这个
Getting_Posts model = dataSnapshot.getValue(Getting_Posts.class);
就像我说的那样,我没有时间测试它,所以可能会有一些错误,但我想给你一些工作。您应该能够在线找到大量有关RecyclerView和RecyclerAdapter的文档,以帮助您解决此问题。