I was trying to retrieve the data from user and time. But I can't access the data, it's not really accessing the database.
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_balance);
mFirebaseDatabase=FirebaseDatabase.getInstance();
cashRef=mFirebaseDatabase.getReference().child("ASSETS").child("cash_at_bank");
TextView view=(TextView)findViewById(R.id.view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cashRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int time=dataSnapshot.getValue(int.class);
int values =dataSnapshot.getValue(int.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
To get the time, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference cashAtBankRef = rootRef.child("ASSETS").child("Cash at bank");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Long time = ds.child("time").child("time").getValue(Long.class);
Log.d("TAG", time + "");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
cashAtBankRef.addListenerForSingleValueEvent(eventListener);
The output will be:
150555043995
//and so on
Related
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'm trying to implement a search function on my recyclerviewer but I'm having some trouble with it. I have created the query and the method for it,but the search does not return anything at all.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
...
...
mDBListener = mDatabaseRef.addValueEventListener(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
mUploads.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Buildings upload = postSnapshot.getValue(Buildings.class);
upload.setKey(postSnapshot.getKey());
mUploads.add(upload);
}
mAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MainActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
mButtonSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String searchText = mSearchView.getText().toString();
firebaseUserSearch(searchText);
}
});
and i have this outside of my onCreate
private void firebaseUserSearch(String searchText) {
Query queryRef = mDatabaseRef.child("Buildings").orderByChild("name").startAt(searchText).endAt(searchText + "\uf8ff");
queryRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren()) {
for (DataSnapshot postsSnapshot : dataSnapshot.getChildren()) {
final Buildings buildings = postsSnapshot.getValue(Buildings.class);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
Is this the correct way to implement it?
I have a textview in MainActivity.java. There are 5 strings (sentences) which are stored in firebase database. What I want is every time the MainActivity.java launches, a new string should be fetched from the database and must be displayed in the textview. But it only fetches the first string. I am not sure how to make this work! Is there any workaround? Any Help is appreciated.
MainActivity.java
public class Gridshow extends AppCompatActivity{
TextView myText;
private DatabaseReference database;
#Override
protected void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
myText = (TextView) findViewById(R.id.Attri);
database = FirebaseDatabase.getInstance().getReference("StringList");
database.child("String1").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String sentName = dataSnapshot.getValue(String.class);
myText.setText(sentName);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Sample son file:
{
"StringList" : {
"String1" : "Valley Rovers",
"String2" : "John Murphy",
"String3" : "Hello all",
"String4" : "See you all",
"String5" : "Hey all"
}
}
The problem is that you're directly saying to FB to retrieve just the first string in here:
database.child("String1").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String sentName = dataSnapshot.getValue(String.class);
myText.setText(sentName);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
You should retrieve the whole list and randomly choose the new string:
ArrayList<String> stringList = new ArrayList<>();
database.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, String> entries = (Map<String, String>) dataSnapshot.getValue();
stringList.addAll(entries.values());
selectString(stringList);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
public void selectString(ArrayList<String> list){
Random ran = new Random();
int x = ran.nextInt(list.size());
myText.setText(list.get(x));
}
use this
database = FirebaseDatabase.getInstance().getReference().child("StringList");
database.addValueEventListener(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String sentName = dataSnapshot.getValue(String.class);
myText.setText(sentName);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Well, in this line:
database.child("String1").addValueEventListener
You are fetching just String1. You should instead use something like this:
database.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(int i = 0;i<dataSnapshot.getChildren().size();i++){
if(i==positionOfStringYouWant){
String sentName = dataSnapshot.getValue(String.class);
myText.setText(sentName);
}
}
}
#Override
public void onCancelled(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);
}
Here is my code:
mFirebaseDatabae = FirebaseDatabase.getInstance();
mMessageReference = FirebaseDatabase.getInstance().getReference().child("memos");
serchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mMessageReference.orderByChild("title").equalTo("z").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(SerchActivity.this, "print", Toast.LENGTH_LONG).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
Here is my Firebase tree:
Isn't it possible for the title to be printed only when the title is z?
Fatch like this way...
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
String title= (String) messageSnapshot.child("title").getValue();
}