I am trying to store userid's into uidMatches. When I do the first println I am getting the correct string array of matches, but when i do the second one it says my uidMatches array has nothing in it... "[ ]". I don't see how this is possible.
private void populateListView() {
final ArrayList<String> uidMatches = new ArrayList<String>();
final ArrayList<Profile> match_profiles = new ArrayList<Profile>();
users = database.getReference("Users");
user = FirebaseAuth.getInstance().getCurrentUser();
matches = database.getReference("Matches").child(user.getUid());
// Returns uid's for all profiles you matched with
matches.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String match = child.getValue().toString();
uidMatches.add(match.toString());
System.out.println("HERE ARE MATCHES 1: " + uidMatches);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
System.out.println("HERE ARE MATCHES 2: " + uidMatches);
// Loop to get all Profiles that match each "uidMatches" uid
for (int x=0; x<uidMatches.size(); x++)
{
users.child(uidMatches.get(x)).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren())
{
Profile profile = child.getValue(Profile.class);
match_profiles.add(profile);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
Picture of my firebase setup. I Blocked out the users unique ID's generated from FB login
This isn't a firebase issue it's simply a Java problem. The println at the end of your code gets run when the code first starts when the array is empty. The other println gets fired onDataChange so after the results are populated.
Related
Here is my code. Also, I am using auto-clicking on index 0, How do I Stop Repeating items when shuffling?
if (ref != null) {
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
list = new ArrayList<>();
String state;
String UserEmail;
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Deal value = ds.getValue(Deal.class);
value.setKey(ds.getKey());
state = "" + ds.child("VideoStatus").getValue();
if (state.equals("incomplete"))
list.add(value);
Collections.shuffle(list);
}
AdapterClassssautoclicker adapterClassssautoclicker = new AdapterClassssautoclicker(list);
recyclerView.setAdapter(adapterClassssautoclicker);
// recyclerView.swapAdapter(adapterClassssautoclicker);
}
int pos = 0;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (recyclerView.findViewHolderForAdapterPosition(pos) != null)
Objects.requireNonNull(recyclerView.findViewHolderForAdapterPosition(pos)).itemView.performClick();
}
}, 1);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
According to your last comment:
Code is repeating Items I want to remove repetition while shuffling
To solve this issue, you have to get the following line of code:
Collections.shuffle(list);
Out of the for loop as you see in the following line of code:
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Deal value = ds.getValue(Deal.class);
value.setKey(ds.getKey());
state = "" + ds.child("VideoStatus").getValue();
if (state.equals("incomplete"))
list.add(value);
}
Collections.shuffle(list);
In this way, you are shuffling the elements only once, and not at every iteration. In this way you can avoid duplicate elements.
How to get the value of this automatically without writing each one separately.
I tried this
FirebaseDatabase.getInstance().getReference().child("Orutmen").child("episodeNumber");
but it shows null value.
This one works, but I have to write for each field:
FirebaseDatabase.getInstance().getReference().child("Orutmen").child("1").child("episodeNumber");
Here is the code
m2Database = FirebaseDatabase.getInstance().getReference().child("Orutmen");
m2Database.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value = snapshot.child("episodeNumber").getValue(String.class);
Toast.makeText(EpisodeListModel.this, value, Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
You can iterate throught the children of DataSnapshot:
m2Database = FirebaseDatabase.getInstance().getReference().child("Orutmen");
m2Database.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
value = snapshot.child("episodeNumber").getValue(String.class);
Toast.makeText(EpisodeListModel.this, value, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
I figured out a way
Just added a list of strings that collects the values from the snapshot
then retrieve the value by position of item clicked >>
List<String> List = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String value = snapshot.getKey();
List.add(value);
}
Toast.makeText(EpisodeListModel.this, List.get(position), Toast.LENGTH_SHORT).show();
I have a problem i get value from Firebase, but the email only fills up after activity run second time.
private ArrayList<String> currentList = new ArrayList<>();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("userTable").child(user.getUid()).child("Container");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
String string = childDataSnapshot.getKey();
currentList.add(string);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Toast.makeText(getContext(), currentList.size(),
// Toast.LENGTH_LONG).show();
String size = String.valueOf(listCurrent.size());
firstNameOfCurrentList.setText(size);
First time size = 0;
Second time = 3(true result);
update UI after getting response.hope now it will work fine for you
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
String string = childDataSnapshot.getKey();
currentList.add(string);
}
//Toast.makeText(getContext(), currentList.size(),
// Toast.LENGTH_LONG).show();
String size = String.valueOf(listCurrent.size());
firstNameOfCurrentList.setText(size);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Firebase listener is async, which means it will run in the background and not stop the code until you recieve a response.
In your code you don't wait for the value to be returned from the server, that why size=0.
what you need to do is check the size inside the listener and after the for loop :
for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
String string = childDataSnapshot.getKey();
currentList.add(string);
}
String size = String.valueOf(listCurrent.size());
firstNameOfCurrentList.setText(size);
This way you will wait for the value to be returned from firebase and then get the list size.
The following is my Firebase database graph:
There are three children of the root element links. When I execute the following loop to get all children, it returns the result for only the first child
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("links");
int count=0;
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("Firebase","Children: "+dataSnapshot.getChildren().toString()); //This gives result for all children
for (DataSnapshot ds : dataSnapshot.getChildren()) {
count++;
}
Log.d("Firebase","Count length: "+count); //Gives only "1"
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
In the above code the count should be 3 but it returns 1. The loop executes only once.
However the following line in the above code works correctly and gives results for all 3 children.
Log.d("Firebase","Children: "+dataSnapshot.getChildren().toString());
Where is the problem? Thanks for your answers!
If you want to get the number of children under links, then try the following:
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("Firebase","Children: "+dataSnapshot.getChildren().toString()); //This gives result for all children
long childCount = dataSnapshot.getChildrenCount();
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
getChildrenCount() will return then number of direct children under links, in this case 3.
I am trying to build a simple friend list, where the user can create some friend groups and each friend group can have a lot of friends, inside the friend group the user can add new friends by contact(inserting the phone number on a editText) the user is added in the list(I use recycleViews), each time I insert a new user in a friendList group in firebase it is added inside the group with the specific name(name can't be repeated for groups) with the userID and the value = true for that user, this is a representation of my firebase:
image the 't' refers to true
what I need, is to display all the users for a specific group, as long as I safe just the id inside my group, I need to get the entire user from that id, but not just 1 user I need to get them all.
So I did this:
private void prepareFriendList() {
myRef.child(id).child("FriendLists").child(group).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
currentFriends.add(snapshot.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
myRef.child(id).child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
for(String item: currentFriends){
if (snapshot.getKey().equals(item)) {
usersList.add(user);
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mAdapter.notifyDataSetChanged();
}