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+")");
Related
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'm calling addValueEventListener inside the button click, but this method only reads the last item in the node. I want to read all the child value in the node What went wrong here?
btnorder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren())
{
SelectedItems si=dataSnapshot1.getValue(SelectedItems.class);
si.getItemname();
Toast.makeText(MyBookedItems.this, ""+si.getItemname(), Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
});
I think you can create an array list and put all snapshot data into it, and then you can find the last data of the list with the value you find by subtracting one from the length of the list. Sorry for my bad English :( I have tried to explain in the solution in the code below)
ArrayList<SelectedItems> selectedItems = new ArrayList<SelectedItems>();
for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren())
{
SelectedItems si=dataSnapshot1.getValue(SelectedItems.class);
selectedItems.add(si);
}
selectedItems.get(selectedItems.size());
It's read all the values but I got the output as a Toast message, because of that I only see the last value)
I am building an app. I have a Firebase database with a bunch of products, each of them has a rating. I am displaying them in a ListView but want to sort them by their rating. Do I need to save them differently to get this to work ?
private ListView mTopRatedList;
ArrayList<String> mAllBeers = new ArrayList<>();
DatabaseReference Reference = FirebaseDatabase.getInstance().getReference().child("Beers");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_rated);
mTopRatedList =(ListView)findViewById(R.id.topRatedList);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mAllBeers);
mTopRatedList.setAdapter(arrayAdapter);
arrayAdapter.clear();
Reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
arrayAdapter.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
//arrayAdapter.add(snapshot.getValue().toString());
Beers beers = snapshot.getValue(Beers.class);
String beerClass = beers.getmName();
Float beerRating = beers.getmRating();
//Collections.sort()
arrayAdapter.add(beerClass+ "User Rating is " + beerRating);
}
arrayAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
From the look of your code, each of your beers has a rating property in the database.
In that case you can show the beers ordered by rating by doing:
Reference.orderByChild("rating").addValueEventListener(new ValueEventListener() {
...
A few notes:
This will show the beers in ascending order of their rating, so with the lowest value first. If you want to show the highest rated beer first, Firebase does not have a built-in operator for descending sort. So you'll have to work around that by either reversing the results in your client-side code, or by including an inverted rating in the database too for this purpose. In the latter case you'd sort on the inverted rating, and then display the regular rating.
For this to work correctly it is important that the rating value is a number, and not stored as a string. When stored as a string it may work for values up to 9, but after that you'll run into cases where numerical ordering differs from the lexicographical ordering that Firebase will use for strings.
Please never leave onCancelled empty as it hides potential problems. Its minimal implementation is public void onCancelled(#NonNull DatabaseError databaseError) { throw databaseError.toException(); }.
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());
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