我想在一个名为“ Likes”的节点下检索数据,然后在其中存储“ postID”(也恰好是发布的键)和一个子“ UID”,其中包含我要检索的所有数据。
我尝试遵循本教程,但未成功。 Android Firebase retrieve data from all nodes (nested) in Fragments
databaseReference = FirebaseDatabase.getInstance().getReference().child("Likes");
mQuery = databaseReference.orderByChild("uid").equalTo(current_user);
protected void onStart() {
super.onStart();
Toast.makeText(this, house+ " "+mAuth.getCurrentUser().getUid(), Toast.LENGTH_LONG).show();
final String user_id = mAuth.getCurrentUser().getUid();
FirebaseRecyclerAdapter<FavHelper, FavouriteActivity.FavViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<FavHelper, FavouriteActivity.FavViewHolder>(FavHelper.class, R.layout.favourite_item, FavouriteActivity.FavViewHolder.class, mQuery) {
@Override
protected void populateViewHolder(final FavouriteActivity.FavViewHolder viewHolder, FavHelper model, int position) {
final String house_id = getRef(position).getKey();
//if (house_id != null) {
viewHolder.setPrice(model.getPrice());
viewHolder.setPlace(model.getPlace());
viewHolder.setLocation(model.getLocation());
viewHolder.setImage(getApplicationContext(), model.getImage());
viewHolder.setFavBtn(house_id);
}
我想显示只有用户喜欢的帖子。find the pic of the database here
答案 0 :(得分:0)
我通过更改数据的存储方式(即,而不是将数据存储在
下)解决了该问题
.child(house_id).child(user_id)
,我用过.child(user_id).child(house_id)
。但是现在我遇到了一个问题,当我尝试保存多个数据时,它将替换现有数据而不是添加数据。 [该应用的屏幕截图][1]我在做什么错了?
我的代码:
viewHolder.favBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mProcessLike = true;
mDatabaseLikes.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (mProcessLike) {
if (mAuth.getCurrentUser() != null) {
String user = mAuth.getCurrentUser().getUid();
if (dataSnapshot.hasChild(user) ) {
mDatabaseLikes.child(user).child(house_id).removeValue();
mProcessLike = false;
} else {
KejaHelper keja = new KejaHelper(getApplicationContext());
TextView loc = findViewById(R.id.txtLocation);
TextView place = findViewById(R.id.txtPlace);
TextView price = findViewById(R.id.txtPrice);
TextView img = findViewById(R.id.txtKey);
// String imag = keja.getImage().toString();
dataSnapshot.getValue();
HashMap<String, Object> map = new HashMap<>();
map.put("name", loc.getText().toString());
map.put("place", place.getText().toString());
map.put("price",price.getText().toString());
map.put("image",img.getText().toString());
map.put("fav", "love");
mDatabaseLikes.child(user).child(house_id).updateChildren(map);
// Toast.makeText(MainActivity.this, , Toast.LENGTH_SHORT).show();
mProcessLike = false;
}
}
}
}
enter code here
enter code here