I want to know how to display this "Hello, World" message from my database to one of my TextViews? Thank you.
If you want to show the value from the Firebase Realtime Database, there 3 ways. But I will show you the simple way.
I continue with your code.
TextView fireBaseTextView;
TextView fireBaseTextView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fireBaseTextView = findViewById(R.id.fireBaseTextView);
fireBaseTextView2 = findViewById(R.id.fireBaseTextView2);
//Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World");
//Display the message from the database
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
final String message = dataSnapshot.getValue(String.class);
fireBaseTextView2.setText(message);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
Related
I want to retrieve data of specific node (clicksRemain) of current online user from firebase into a textView but the data is not showing. Below is my code.
PracticeActivity
public class PracticeActivity extends AppCompatActivity {
TextView link1;
DatabaseReference dRef;
FirebaseDatabase firebaseDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practice);
link1 = findViewById(R.id.link1);
firebaseDatabase = FirebaseDatabase.getInstance();
dRef = FirebaseDatabase.getInstance().getReference();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
dRef.child("clicksRemain").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
String remainingClicks = snapshot.getValue(String.class);
link1.setText(remainingClicks);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}else{
link1.setText("10");
}
}
}
I think the code is correct to check if a user is logged in or not as if a user is logout then the app shows the value 10 as written in this code.
Snapshot of database
Thanks in advance
I have done these changes and now successfully retrieved the data.
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uID = user.getUid();
dRef = FirebaseDatabase.getInstance().getReference("Users");
dRef.child(uID).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
String remainingClicks = String.valueOf(snapshot.child("clicksRemain").getValue(Integer.class));
link1.setText(remainingClicks);
}
When running the app the data is not retrieved but when debugging i can see where it is in terms of the database link.
When it runs and get to addValueEventListener, it doesn't go into the function which is pretty weird, don't know if this is because its async or not but either way data from Firebase does not get retrieved.
public GarbageItems(int itemNum) {
String itemId = String.valueOf(itemNum);
database = FirebaseDatabase.getInstance();
gameObjectRef = database.getReference().child("gameObjects");
itemInformationGrabber(gameObjectRef, itemId);
}//GarbageItems(Constructor)
private void itemInformationGrabber(DatabaseReference gameObjectRef, String itemId) {
DatabaseReference dataReference = gameObjectRef.child(itemId);
DatabaseReference itemColor = dataReference.child("Color");
itemColor.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange( DataSnapshot dataSnapshot) {
String color = dataSnapshot.getValue(String.class);
setColor(color);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
This is in a different classs that calls GarbageItem
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
colorTextView = findViewById(R.id.ColorTextView);
itemTextView = findViewById(R.id.ItemNameTextView);
GarbageItems garbageItems = new GarbageItems(1);
Color = garbageItems.getColor();
}
try this...
private void itemInformationGrabber(DatabaseReference gameObjectRef, String itemId) {
DatabaseReference dataReference = gameObjectRef.child(itemId);
DatabaseReference itemColor = dataReference.child("Color");
itemColor.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange( DataSnapshot dataSnapshot) {
for(Datasnapshot dataSnap : dataSnapshot.getChildren()){
String color = dataSnap.child("Color").getValue(String.class);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
Hope it helps you :)
I had already done on the data update part and now i would like to extract the data and display it based on user input. I have a edit text column for user to key-in and text view to display the data that i stored in Firebase. May i know what is the best coding to work on this ? I found alot of them are using DataSnapshot but they directly read specific value from android studio while i would like to read the value from user input.[https://i.stack.imgur.com/aYrOF.png]
Edittext key in -> 978
TextView shows = 12
tv_displayIsland=(TextView)findViewById(R.id.tv_displayIsland);
et_scanIsbn=(EditText)findViewById(R.id.et_scanIsbn);
database= FirebaseDatabase.getInstance().getReference();
database.addValueEventListener(new ValueEventListener() {
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
-- need the coding to read the et_scanIsbn and display in tv_displayIsland--
}
}
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
You can retrieve the value of et_scanIsbn by doing the following:
String sbn = et_scanIsbn.getText().toString();
Then you can do:
database.child("Island").orderByChild("isbn").equalTo(sbn).addValueEvent....
Inside the onDataChange you can display the retrieved value inside the TextView:
for(DataSnapshot ds : dataSnapshot.getChildren()){
String island = ds.child("island").getValue(String.class);
tv_displayIsland.setText(island);
This is my code now.
package com.example.scanning;
public class Scan extends AppCompatActivity {
DatabaseReference database;
TextView tv_displayIsland;
EditText et_scanIsbn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
tv_displayIsland = (TextView) findViewById(R.id.tv_displayIsland);
et_scanIsbn = (EditText) findViewById(R.id.et_scanIsbn);
String sbn = et_scanIsbn.getText().toString();
database = FirebaseDatabase.getInstance().getReference();
database.child("Island").orderByChild("isbn").equalTo(sbn).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String island = ds.child("island").getValue(String.class);
tv_displayIsland.setText(island);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
I have data in database:
database in firebase
I want read all child (Belgia, Czechy, Polska...) and display it in Text Field, but AFTER click the button (I don't change data in database).
The button have assigned below function:
public void f_btn_zaczytaj_dane(View view) {
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value = dataSnapshot.getValue(String.class);
TextView txt_opis_panstwa = (TextView) findViewById(R.id.txt_opis_panstwa);
txt_opis_panstwa.setText(value);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Befrore i write in code:
FirebaseDatabase database;
DatabaseReference myRef;
database = FirebaseDatabase.getInstance();
myRef = database.getReference("kraj");
Unfortunately, when I press the button nothing happens.
I will be grateful for the help
Regards, Marcin
You have to iterate through your children
public void f_btn_zaczytaj_dane(View view) {
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
TextView txt_opis_panstwa = (TextView) findViewById(R.id.txt_opis_panstwa);
for (DataSnapshot country : dataSnapshot.getChildren()) {
txt_opis_panstwa.append(country.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Please use this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference krajRef = rootRef.child("flagiistolice").child("kraj");
public void f_btn_zaczytaj_dane(View view) {
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
TextView txt_opis_panstwa = (TextView) findViewById(R.id.txt_opis_panstwa);
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String country = ds.getkey();
txt_opis_panstwa.append(country);
Log.d("TAG", country);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
krajRef.addListenerForSingleValueEvent(eventListener);
}
I am trying to populate a TextView from a firebase database. Here is the sample json file.
{
"Player" : {
"Club" : "Valley Rovers",
"Name" : "John Murphy"
}
}
Here is my android code:
public class MainActivity extends AppCompatActivity {
private TextView mPlayer;
private DatabaseReference mDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlayer = (TextView) findViewById(R.id.player);;
mDatabase = FirebaseDatabase.getInstance().getReference("Player").child("Name");
final String player = mDatabase.push().getKey();
mDatabase.child("Name").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Check for null
if (player == null) {
Log.e(TAG, "User data is null!");
return;
}
mPlayer.setText(player);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
I want to add the name John Murphy to the firebase database and doing so the TextView mPlayer will populate with John Murphy.
You are getting the child "Name" twice on your code (leading to Player/Name/Name). Remove it from the mDatabase initialization:
mDatabase = FirebaseDatabase.getInstance().getReference("Player");
And you're never really getting the value from the DataSnapshot received. To do so, use this:
mDatabase.child("Name").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String playerName = dataSnapshot.getValue(String.class);
mPlayer.setText(playerName);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
In case someone wants to use POJO approach to this problem in future, follow the example below:
Remove the child("Name") from mDatabase.child("Name").addValueEventListener(new ValueEventListener()
unless you want to fetch the "Name" child only. Also remember to effect the correction made on mDatabase initialization.
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// String playerName = dataSnapshot.getValue(String.class);
PlayerModel playerModel = dataSnapshot.getValue(PlayerModel.class);
((TextView)findViewById(R.id.textviewName)).setText(playerModel.getName());
((TextView)findViewById(R.id.textviewClub)).setText(playerModel.getClub());
// mPlayer.setText(playerName);
}