How to find firebase all children details from different users? - java

I want to show all product child details. but I was unable to do so. I am new to firebase and wish to complete my university project I need to use firebase.
each user has a single or multiple products. I can show all shops from a single user but I want to show all products from all user.
private void loadShopProducts() {
productList = new ArrayList<>();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.child(shopUid).child("Products")
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
productList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()){
ModelProduct modelProduct = ds.getValue(ModelProduct.class);
productList.add(modelProduct);
}
adapterProductBuyer = new
AdapterProductBuyer(ShopDetailsActivity.this, productList);
productRv.setAdapter(adapterProductBuyer);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}

Basically, I need to pull out all the children from users who are the seller.
private void loadAllProduct(){
productList = new ArrayList<>();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
productList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()){
String uid = ""+ds.getRef().getKey();
DatabaseReference ref = (DatabaseReference) FirebaseDatabase.getInstance().getReference("Users").child(uid).child("Products");
// ref.addValueEventListener(new ValueEventListener() {
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
productList.clear();
if (dataSnapshot.exists()){
for (DataSnapshot ds : dataSnapshot.getChildren()){
ModelProduct modelProduct = ds.getValue(ModelProduct.class);
productList.add(modelProduct);
}
adapterProductShow = new AdapterProductShow(MainBuyerActivity.this, productList);
showAllProductRv.setAdapter(adapterProductShow);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

Related

How to exclude data entry in ArrayList using adapter from firebase?

I have 2 columns in my firebase (kamar and kamar_terisi), every time a user rents a room, the room key automatically enters the kamar_terisi column, and I want to make it not appear on this list,
It must be not appear in list
This my code
private void showKamar() {
list = new ArrayList<>();
list2 = new ArrayList<>();
reference = FirebaseDatabase.getInstance().getReference("Kamar");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
list.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
final Kamar kamar = snapshot.getValue(Kamar.class);
kamar.setKey(snapshot.getKey());
list.add(kamar);
isi_reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (final DataSnapshot snapshot : dataSnapshot.getChildren()) {
final KamarIsi kamarIsi = snapshot.getValue(KamarIsi.class);
kamarIsi.setKey(snapshot.getKey());
isi_reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (snapshot.hasChild(kamarIsi.getKey())) {
list2.add(kamarIsi);
} else if (!kamarIsi.getId_user().equals("")) {}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
kamarListAdapter = new KamarListAdapter(getContext(), list, list2);
sewakamar.setAdapter(kamarListAdapter);
progressDialog.dismiss();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
System.out.println(databaseError.getDetails()+" "+databaseError.getMessage());
progressDialog.dismiss();
}
});
}

Compare two different child nodes from two different parent nodes

I am trying to compare two different child nodes from two different parent nodes. The JSON structure is the following: Attending Event > postId > userId and the second one is structured as follows: Following > firebaseUserId > userId. The goal here is to compare the userId's and if I am following that userId and also that person / userId IS attending the event, then I want to populate a list with those users.
I already wrote two methods, one to get the users that I am following getFollowing(); and another to get the users that are attending the event getAttendingEvent();. Is there a way to compare the lists / usersIds from the two methods I have already written, or do I have to write a new method entirely?
I´m really not sure how to go about this… Am I on the right track with the getFriendsAttendingEvent(); method I just wrote?
FollowersActivity
public class FollowersActivity extends AppCompatActivity {
String mId;
String mTitle;
String mPostId;
List<String> mIdList;
RecyclerView mRecyclerView;
UserAdapter mUserAdapter;
List<User> mUserList;
FirebaseUser mFirebaseUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_followers);
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
Intent intent = getIntent();
mId = intent.getStringExtra("id");
mTitle = intent.getStringExtra("title");
mPostId = intent.getStringExtra("postid");
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(mTitle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(v -> finish());
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mUserList = new ArrayList<>();
mUserAdapter = new UserAdapter(this, mUserList, false);
mRecyclerView.setAdapter(mUserAdapter);
mIdList = new ArrayList<>();
switch (mTitle) {
case "Likes":
getLikes();
break;
case "Following":
getFollowing();
break;
case "Followers":
getFollowers();
break;
case "Attending Event":
getAttendingEvent();
case "Friends Attending":
// Create this method
}
}
private void getFriendsAttendingEvent(List<User> userList, List<User> userList1) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Attending Events").child(mPostId);
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
userList.add(user);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
DatabaseReference reference1 = FirebaseDatabase.getInstance().getReference("Following").child(mFirebaseUser.getUid());
reference1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user1 = snapshot.getValue(User.class);
userList1.add(user1);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
// Compare the two lists user's somehow..
if (userList.equals(userList1)) {
showUsers();
}
}
private void getLikes() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Likes").child(mPostId);
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
mIdList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
mIdList.add(snapshot.getKey());
}
showUsers();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void getAttendingEvent() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Attending Event").child(mPostId);
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
mIdList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
mIdList.add(snapshot.getKey());
}
showUsers();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void getFollowing() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Following").child(mFirebaseUser.getUid());
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
mIdList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
mIdList.add(snapshot.getKey());
}
showUsers();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void getFollowers() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Followers").child(mId);
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
mIdList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
mIdList.add(snapshot.getKey());
}
showUsers();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void showUsers() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
mUserList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
for (String id : mIdList) {
if (user != null)
if (user.getId().equals(id)) {
mUserList.add(user);
}
}
}
mUserAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}

How to change to a ValueEventListener that i can remove?

I have a Firebase valueEvent listener:
questions.orderByChild("CategoryID").equalTo(categoryID)
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Question ques = postSnapshot.getValue(Question.class);
Common.questionList.add(ques);
}
//Random List
Collections.shuffle(Common.questionList);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
As its best practice to have an object that can be attached and removed, i want to change the code to a ValueEventListener object. I did the following:
mQuestionsListener = questions.orderByChild("CategoryID").equalTo(categoryID)
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Question ques = postSnapshot.getValue(Question.class);
Common.questionList.add(ques);
}
//Random List
Collections.shuffle(Common.questionList);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
questions.addValueEventListener(mQuestionsListener);
but is not working.
Where am i wrong?
You already added a listener here questions.addValueEventListener(mQuestionsListener);, then you need to declare a new ValueEventListener():
ValueEventListener mQuestionsListener = new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Question ques = postSnapshot.getValue(Question.class);
Common.questionList.add(ques);
}
//Random List
Collections.shuffle(Common.questionList);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

Firebase read subdirectory in every entry

How can I get a list of all "Floor" elements in my database. If I use the following database my list should have 3 elements. List should contain the elements with the id's 1234, 4321, 2341.
mydatabase
-buildings
--LNBxRoNBhVZyXniqe9t
---checked
---anzI
---anzA
---floors
----f1
-----1234
------description
------id
----f2
-----4321
------description
------id
--LXdsafRfasdf12asdfJ
---checked
---anzI
---anzA
---floors
----f1
-----2341
------description
------id
This is my DAO:
private FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("buildings");
#Override
public void initialize() {
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
List<Floors> list = new LinkedList<>();
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
????
}
#Override
public void onCancelled(DatabaseError error) {
Log.w(TAG, "Failed to read value.", error.toException());
}});
You will need to traverse the DataSnapshot that you get from Firebase. Something like this
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot buildingSnapshot: dataSnapshot.getChildren()) {
for (DataSnapshot floorSnapshot: buildingSnapshot.child("floors").getChildren()) {
for (DataSnapshot numberSnapshot: floorSnapshot.getChildren()) {
Log.i(TAG, numberSnapshot.getKey()); // "1234", "4321", "2341"
Log.i(TAG, ""+numberSnapshot.child("id").getValue());
}
}
}
}
#Override
public void onCancelled(DatabaseError error) {
Log.w(TAG, "Failed to read value.", error.toException());
}
});
How can I get a list of all "Floor" elements in my Database?
In a very simple way, by iterating the database using getChildren() method three times.
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference buildingsRef = rootRef.child("buildings");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> list = new ArrayList<>();
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot ds : dSnapshot.child("floors").getChildren()) {
for(DataSnapshot d : ds.getChildren()) {
String key = d.getKey();
String description = d.child("description").getValue(String.class);
list.add(key);
Log.d("TAG", key);
}
}
}
//Do what you need to do with your list
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage());
}
};
buildingsRef.addListenerForSingleValueEvent(valueEventListener);
List should contain the elements with the id's 1234, 4321, 2341
The list will contain those exact three ids. If you try to print the list in your logcat, the result will be:
[1234, 4321, 2341]

How i can get a value in firebase

I have a database that looks like this:
And my code like this:
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot uniqueKey:dataSnapshot.getChildren()) {
for (DataSnapshot keysiswa:uniqueKey.getChildren()) {
for (DataSnapshot valuesiswa:keysiswa.child("nama_siswa").getChildren()) {
String c = valuesiswa.getValue().toString();
Log.i(TAG,c);
}
}
}
}
I want to take the value of "nama_siswa" but nothing happens in logcat. What is wrong with my code?
Try this code.
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("siswa");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("Name:",dataSnapshot.getValue(User.class).name); // define your pojo class
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
To get the value of nama_siswa property, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference siswaRef = rootRef.child("siswa");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String nama_siswa = ds.child("nama_siswa").getValue(String.class);
Log.d("TAG", nama_siswa);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
siswaRef.addListenerForSingleValueEvent(valueEventListener);
The output will be:
chandra
coba

Categories

Resources