My Firebase Realtime Database structure
I've made two apps in a eBook project, one for the admin and the other for the users.
When admin gives input to a book's data on Firebase then the user will be able to see the information provided by admin in his app.
I did this on the user app but the admin Id still has the following key's value is null.
And when I was debugging the app, my DataSnapshot
showed me the value null of the key.
My app debugging mode:
when I get admin Uid but it show me null
when I set admin id in database Reference and its get Uid
and I'm get admin Id throw database reference but it dataSnapshot key's value is null
Here is my code:
private String adminId;
private Button button1,button2,button3;
private RecyclerView bookListRv;
private ArrayList<Book> bookList;
private BookAdapter bookAdapter;
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
public BookListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_book_list, container, false);
init(view);
adminId = firebaseAuth.getUid();
configRv();
getBooks();
return view;
}
private void init(View view) {
bookListRv = view.findViewById(R.id.bookRVId);
firebaseAuth = firebaseAuth.getInstance();
databaseReference = FirebaseDatabase.getInstance().getReference();
bookList = new ArrayList<>();
bookAdapter = new BookAdapter(bookList, getContext());
}
private void configRv() {
bookListRv.setLayoutManager(new LinearLayoutManager(getContext()));
bookListRv.setAdapter(bookAdapter);
}
private void getBooks(){
DatabaseReference showBookRef = databaseReference.child("Books").child(adminId);
showBookRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot data:dataSnapshot.getChildren()){
Book book = data.child("BookInfo").getValue(Book.class);
bookList.add(book);
bookAdapter.notifyDataSetChanged();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Any help me for appreciated
Observations:
BookInfo
under admin UID - oJ26Q...G3
BookInfo
using user UID - 0FX6M...
Conclusions:
BookInfo
using user UID.Possible Solution: Upload the BookInfo
in a common place [irrespective of user], so that every user gets access the BookInfo
as soon as admin upload it.
Then query it like below:
databaseReference.child("Books").addValueEventListener
Workaround in current situations: You have to use the adminId
as hardcoded to get the BookInfo
. Check below:
private void getBooks(){
DatabaseReference showBookRef = databaseReference.child("Books").child("oJ26QDoFZBWAgWjxbFOX5jv1tcG3");
showBookRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot data:dataSnapshot.getChildren()){
Book book = data.child("BookInfo").getValue(Book.class);
bookList.add(book);
bookAdapter.notifyDataSetChanged();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments