Compare two different child nodes from two different parent nodes - java

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

Related

How to find firebase all children details from different users?

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

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();
}
});
}

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

How to retrieve data from firebase , without changes in the data?

// This is the MainActivity, I have used firebase to store sample data, where I can update and retrieve.
But retrieval of data is possible only when there is a data change,but I want to see my data which is in firebase already
here is the data base structure]1
public class MainActivity extends
AppCompatActivity {
EditText name,weight;
Button uploadBtn;
TextView itemName,itemWeight;
DatabaseReference databaseReference=FirebaseDatabase.getInstance().getReference();
Map<String,String>values=new HashMap<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=findViewById(R.id.inputItemName);
weight=findViewById(R.id.inputItemWeight);
uploadBtn=findViewById(R.id.uploadButton);
itemName=findViewById(R.id.resultName);
itemWeight=findViewById(R.id.resultWeight);
// Here as you can see I have used an ValueEventListener to show the retrieve values from database before uploading the new Values But it is not working , unless the values of data are changed (that is it works only after uploading new values).
ValueEventListener valueEventListener=new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String nameD=(String) dataSnapshot.child("Name").getValue();
String weightD = (String) dataSnapshot.child("Weight").getValue();
itemName.setText(nameD);
itemWeight.setText(weightD+" Kg");
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
};
databaseReference.addListenerForSingleValueEvent(valueEventListener);
uploadBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
values.put("Name",name.getText().toString());
values.put("Weight",weight.getText().toString());
databaseReference.child("UToouch").setValue(values);
}
});
databaseReference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
String nameDb=(String) dataSnapshot.child("Name").getValue();
String weightDb = (String) dataSnapshot.child("Weight").getValue();
itemName.setText(nameDb);
itemWeight.setText(weightDb+" Kg");
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
To be able to get values of your Name and Weight properties, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("UToouch");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("Name").getValue(String.class);
String weight = dataSnapshot.child("Weight").getValue(String.class);
Log.d(TAG, name + " / " + weight);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
ref.addListenerForSingleValueEvent(valueEventListener);
The output in your logcat will be:
rittesh / pv

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