firstly, i want to build navigator drawer. i have already call text from firebase but my problem is how to display in nav_header. i dont know how to explain in specific. I want key in first name and last name but i dont know where to call class .i try show my code.
This how i call string from firebase but now i try to display in navigator bar
userDatabase = new UserDatabase();
reff = FirebaseDatabase.getInstance().getReference("Member").child(userid);
reff.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snap: dataSnapshot.getChildren()){
UserDatabase user = snap.getValue(UserDatabase.class);
txvName.setText(""+user.getFirstName()+" "+user.getLastName());
txvAdd.setText(""+user.getAddress1()+" "+user.getAddress2()+" "+user.getAddress3());
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
that so many xml and that only one of class is sidemenu.class . I dont know where to put the code in sidemenu.class
You can call inside activity which contains Navigation drawer
and add menu item like this
final Menu menu = navigationView.getMenu();
menu.add(user.getFirstName()+" "+user.getLastName());
Related
So, I am using android studio to make an app that displays data stored in firebase real-time database. The app is simple, it displays the name and phone number from firebase to the app.
The app works fine and even displays the data but the only thing is it displays the data/values with curly braces and an equal sign (like a JSON format) is there anyway I can make it display just the desired values and not the extra signs and punctuation
Code:
ArrayList<String> list =new ArrayList<>();
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_view, list);
listView.setAdapter(adapter);
DatabaseReference ref= FirebaseDatabase.getInstance().getReference().child("Car Wash");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
list.clear();
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
list.add(snapshot.getValue().toString());
}
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
What you're seeing is the toString() output of the value a Firebase DataSnapshot that represents an object with multiple values.
You'll want to get the individual values from that snapshot, and display their values with something like this:
String name = snapshot.child("Name").getValue(String.class);
String phoneNr = snapshot.child("Phone Number").getValue(String.class);
list.add(name+" ("+phoneNr+")");
I want to make two database calls one inside the other and make use of the first call data. Since firebase database calls are asynchronous this is not behaving the way I want.
I HAVE two database nodes Users, Chats.
I want first query through one Users at a time and take the user id and then query through the Chats model and check if the id in Users and Chats nodes are same
Something like this.
database.getReference().child("Users").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
list.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
users = dataSnapshot.getValue(Users.class);
users.setUserId(dataSnapshot.getKey());
database.getReference().child("Chats").child(auth.getUid() + users.getUserId()).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
//ADD USER TO LIST UPDATE UI
list.add(users);
}
else{
//DON'T ADD USER
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
Hope I made the problem clear.
I have written this code to fetch the data. But I want to fetch updates without restarting the activity as data changes in real-time in the database.
I want to show data dynamically:
FirebaseDB.batch.child(batch).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
//Update recyclerview here
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
If you need to get data in real-time, you should use a real-time listener. To solve this, please change your code to:
FirebaseDB.batch.child(batch).addValueValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
//Update recyclerview here
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Log.d("TAG", error.getMessage()); //Never ignore potential errors!
}
});
See, I have used Query's addValueEventListener():
Add a listener for changes in the data at this location.
instead of addListenerForSingleValueEvent() which:
Add a listener for a single change in the data at this location.
You are using wrong listener. According to firebase documentation this listener will be triggered only once.
You need to use simple ValueEventListener as stated in docs
My app has an activity that shows a news article and it has a bookmark ImageButton. When clicked, the method shown below is called
public void onBookmarkClick(View view){
if (isBookmarked){
bookmarkedDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Article articleValue = snapshot.getValue(Article.class);
String id = snapshot.getKey();
if (article.equals(articleValue)){
bookmarkedDatabase.child(id).removeValue();
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}else {
String id = bookmarkedDatabase.push().getKey();
bookmarkedDatabase.child(id).setValue(article);
}
isBookmarked = !isBookmarked;
setupBookmarkIcon();
}
But when I rotate the screen or press back button immediately after clicking bookmark button, the article isn't added to bookmarks.
How do I make sure that the process of reading and writing from/ to a firebase database is complete even when activity is being destroyed.
You can use complete Listener to know if your update is successful or not . From firebase documentation
Add a Completion Callback
If you want to know when your data has been committed, you can add a completion listener.
Both setValue() and updateChildren() take an optional completion
listener that is called when
the write has been successfully committed to the database.
If the call was unsuccessful, the listener is passed an error object indicating why the failure occurred
Firebase
Or my suggestion to use viewModel class and do update from there.
Viewmodel
I am trying to make it so that when a user creates an open game, in my Firebase Database the name of the game is the size of the list. So the very first game created the name of it will = 0, and if another user creates a game then the game will be labeled 1 and so on.
I have it set up right now so that the games are labeled the size of the game list, but the list isn't really updating. The games keep getting called '0' because it thinks the list is empty even though I have visual confirmation in the app that there are items being added to the list.
So my question is: How can I make it so the list continuously updates each time a game is added, and how can I make it so that it updates for all users and not just the user who created the game?
This is what I have setup right now. Here are the variables I am using for the list and the integer getting the list size
ArrayList<String> openGames = new ArrayList<>();
int gameSlot = openGames.size();
Here is what I use to name the game when it is created.
gameMaker = new GameMaker(hp.uid, userName, wagerD, gameSlot);
FirebaseDatabase.getInstance().getReference("FCGames").child(Integer.toString(gameSlot))
.setValue(gameMaker).addOnCompleteListener...
And this is what I have to add the game to the list.
cgRef.child(Integer.toString(gameSlot)).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
openGames.add(userName);
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
So again my question is how can I make this list update correctly and how can I make it update for all users on the app?
Edit
Here is what I did with my onChangeData
cgRef.child(Integer.toString(gameSlot)).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
wager = (String) dataSnapshot.child("wager").getValue();
gameSlot = openGames.size();
adapter.notifyDataSetChanged();
}
and now the openGames.add is in my createGameLobby method.
FirebaseDatabase.getInstance().getReference("FCGames").child(Integer.toString(gameSlot))
.setValue(gameMaker).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
openGames.add(userName);
Toast.makeText(FlipCoinLobby.this, "Game creation successful.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(FlipCoinLobby.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
^^ that is just the important snippit from the method. And then I have an onClickListener that creates that calls that method when a button is pressed
In below code segment, you did that openGames.add(username) in onDataChange listener. I think it is an incorrect use of this ondatachange function
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
openGames.add(userName);
adapter.notifyDataSetChanged();
}
please use this to get data from db and update your data. Then push your data to db. You didn't use snapshot value also. You can get most recently updated data from dataSnapshot . Use it to update user data then push it to db