onCreate() method is never executed. I just want to take the below data to 4 TextViews. "Detail" is the model class.
No errors are shown when running the app.
view_temp is an activity.
this is firebase realtime db
this is the java class
`public class view_temp extends AppCompatActivity {
private view_temp viewTemp;
public Detail detail;
private TextView roomtemp, roomhuminity, bodypulse, bodytemp;
private DatabaseReference mDatabase;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_temp);
mDatabase = FirebaseDatabase.getInstance().getReference("heat-stroke-device");
roomtemp = (TextView) findViewById(R.id.txt_room_temp);
roomhuminity = (TextView) findViewById(R.id.txt_huminity_temp);
bodypulse = (TextView) findViewById(R.id.txt_body_pulse);
bodytemp = (TextView) findViewById(R.id.txt_body_temp);
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
detail = postSnapshot.getValue(Detail.class);
String rtemp = detail.getRoomtemp();
String rhumidity = detail.getRoomhumidity();
String bpulse = detail.getBodypulse();
String btemp = detail.getBodytemp();
roomtemp.setText(rtemp);
roomhuminity.setText(rhumidity);
bodypulse.setText(bpulse);
bodytemp.setText(btemp);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getMessage());
}
});
}
}`
There is no need to add the name of the project in the getReference() method. To get those names and the corresponding values correctly, please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.getKey();
double value = ds.getValue(Double.class);
Log.d("TAG", name "/" + value);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
}
};
rootRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat will be:
RoomHumi/86.0
RoomTemp/30.8
bodyPulse/126.0
bodyTemp/29.0
If the keys are always fixed, then simply use:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
double roomHumi = dataSnapshot.child("RoomHumi").getValue(Double.class);
Log.d("TAG", "RoomHumi" "/" + valueroomHumi);
double roomTemp = dataSnapshot.child("RoomTemp").getValue(Double.class);
Log.d("TAG", "RoomTemp" "/" + roomTemp);
double bodyPulse = dataSnapshot.child("bodyPulse").getValue(Double.class);
Log.d("TAG", "bodyPulse" "/" + bodyPulse);
double bodyTemp = dataSnapshot.child("bodyTemp").getValue(Double.class);
Log.d("TAG", "bodyTemp" "/" + bodyTemp);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
}
};
rootRef.addListenerForSingleValueEvent(valueEventListener);
And you'll have the same result in the logcat.
Just change your line
mDatabase = FirebaseDatabase.getInstance().getReference("heat-stroke-device");
to
mDatabase = FirebaseDatabase.getInstance().getReference();
Note: You do not need to put your database name into reference.
Related
I have written the following code for searching the specific node and it does work correctly.
private void SearchFirebase() {
Rootref.orderByChild("IMG").equalTo(imUrl).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot product : snapshot.getChildren()){
id = product.getKey(); //returns the id(for eg. 8006)
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
But I don't know how to access the child key-value pairs once the specific parent key is known
In the picture above I need to fetch the key-value pairs such as Product Name or Product Discount etc.
You just have to add child to your reference and get data as explained in this answer
private void SearchFirebase() {
Rootref.orderByChild("IMG").equalTo(imUrl).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot product : snapshot.getChildren()){
id = product.getKey();
Log.i(TAG, Rootref.child(id).child("Product Discount").getValue(String.class));
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
When querying a Firebase Realtime Database, the most convenient way for getting the data out is by calling the right child directly from "DataSnapshot" object and converting it to the right type, as explained in the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference productsRef = rootRef.child("Products");
Query imgUrlQuery = productsRef.orderByChild("IMG").equalTo(imUrl);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String productDiscount = ds.child("Product Discount").getValue(String.class);
String productId = ds.child("Product Id").getValue(String.class);
String productName = ds.child("Product Name").getValue(String.class);
String productPrice = ds.child("Product Price").getValue(String.class);
String shopName = ds.child("Shop Name").getValue(String.class);
Log.d("TAG", productDiscount + "/" + productId + "/" + productName + "/" + productPrice + "/" + shopName);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
}
};
imgUrlQuery.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat will be:
0/8006/Frooti/50/shop-A
My database looks as follows, in here I want to retrieve the data inside the mylocation child (see the attached image here). But it is not working.
How to solve that?
My current code looks as this,
private void loadAddress() {
DatabaseReference ref= FirebaseDatabase.getInstance().getReference("Users");
ref.orderByChild("uid").equalTo(firebaseAuth.getUid())
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
String name=""+ds.child("name").getValue();
String lat=""+ds.child("name2").getValue();
addresstv.setText(name);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
To get the data under mylocation, you use an explicit call to that child, like in the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("mylocation").child("name2").getValue(String.class);
double latitude = dataSnapshot.child("mylocation").child("latitude").getValue(Double.class);
double longitude = dataSnapshot.child("mylocation").child("longitude").getValue(Double.class);
Log.d("TAG", name + "/" + latitude + "/" + longitude);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat will be:
5555/79.8571577/6.9448882
I have a problem, i cant get my data values.. i dont know why.. can anybody help me please??
and this is my code.. i already get values maybe 2 until 3 times.. whats make my bottom code doesnt work in log? huh.. i'm so confused
public class transactioncheckout_page extends AppCompatActivity {
private DatabaseReference mDatabase;
private DatabaseReference dDatabase;
TextView txtProductName,txtTotalProduct,txtPaymentMethod,txtProductPrice,txtValueOfSubtotal, txtValueOfTotal;
String productprice;
int productpriceint;
Button btConfirmTransaction;
int valueofsubtotal,powqtyint;
String powcode, powqty;
long id = 0;
transactions trans;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transactioncheckout_page);
Intent i = getIntent();
final String productname = i.getStringExtra("productname");
final String totalproduct = i.getStringExtra("totalproduct");
String paymentmethod = i.getStringExtra("paymentmethod");
txtProductName = findViewById(R.id.txtProductName);
txtProductName.setText(productname);
txtTotalProduct = findViewById(R.id.txtTotalProduct);
txtTotalProduct.setText(totalproduct+"X");
txtPaymentMethod = findViewById(R.id.txtPaymentMethodValue);
txtPaymentMethod.setText(paymentmethod);
txtProductPrice = findViewById(R.id.txtProductPrice);
txtValueOfSubtotal = findViewById(R.id.txtValueOfSubtotal);
txtValueOfTotal = findViewById(R.id.txtValueOfTotal);
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("PRODUCTS").orderByChild("productname").equalTo(productname).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
productprice = ds.child("productprice").getValue(String.class);
powcode = ds.child("powdercode").getValue(String.class);
System.out.println("POWDER CODE IS " + powcode);
System.out.println("the value of price is " + productprice);
txtProductPrice.setText("(#Rp."+ productprice + ")");
productpriceint = Integer.parseInt(productprice);
valueofsubtotal = Integer.parseInt(totalproduct) * productpriceint;
txtValueOfSubtotal.setText("Rp. " + Integer.toString(valueofsubtotal));
txtValueOfTotal.setText("Rp. " + Integer.toString(valueofsubtotal));
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
trans = new transactions();
mDatabase = FirebaseDatabase.getInstance().getReference().child("TRANSACTIONS");
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
id = (dataSnapshot.getChildrenCount());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
btConfirmTransaction = findViewById(R.id.btConfirmTransaction);
btConfirmTransaction.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String pname = txtProductName.getText().toString();
final String sumofp = totalproduct;
final String totalprice = Integer.toString(valueofsubtotal);
String currentuser = FirebaseAuth.getInstance().getCurrentUser().getEmail();
final String userid = currentuser;
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date ddate = new Date();
String strdate = dateFormat.format(ddate).toString();
final String date = strdate;
final String ppaymentmethod = txtPaymentMethod.getText().toString();
trans.setProductname(pname);
trans.setSumofproduct(sumofp);
trans.setTotalprice(totalprice);
trans.setPaymentmethod(ppaymentmethod);
trans.setPowdercode(powcode);
System.out.println("Powder code = " + powcode);
trans.setUser(userid);
trans.setDate(date);
mDatabase.child("TRANS" + (id+1)).setValue(trans);
Toast.makeText(transactioncheckout_page.this, "Data inserted", Toast.LENGTH_LONG).show();
}
});
mDatabase = FirebaseDatabase.getInstance().getReference("POWDERS");
System.out.println("ABC0000000000000000000000000000000000000000000000000000000");
mDatabase.child("powdername").equalTo(productname).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
powqty = ds.child("powderquantity").getValue(String.class);
// powqtyint = Integer.parseInt(powqty);
// powqtyint = powqtyint - (Integer.parseInt(sumofp)*100);
// powqty = Integer.toString(powqtyint);
System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + powqty);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
I have troubled in this code.. cant get the value :(
i want to get value because i want to minus my powders stock..
mDatabase = FirebaseDatabase.getInstance().getReference("POWDERS");
System.out.println("ABC0000000000000000000000000000000000000000000000000000000");
mDatabase.child("powdername").equalTo(productname).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
powqty = ds.child("powderquantity").getValue(String.class);
// powqtyint = Integer.parseInt(powqty);
// powqtyint = powqtyint - (Integer.parseInt(sumofp)*100);
// powqty = Integer.toString(powqtyint);
System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + powqty);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
this is my database pic
You seem to want to query the data for all child nodes under POWDERS where the powdername property has a certain value.
You do that in Firebase with:
mDatabase = FirebaseDatabase.getInstance().getReference("POWDERS");
mDatabase.orderByChild("powdername").equalTo(productname).addValueEventListener(new ValueEventListener() {
...
Please spend some time studying the Firebase documentation on ordering and filtering data, as it should explain this type of use-case pretty well.
Aside from that, I highly recommend storing your numeric values as actual numbers in the database, as it prevents having to do error-prone conversions in your code.
I'm trying to retrieve a specific value from a Firebase realtime Database.
The data looks like this:
I want to get the URL value from any of them and then put it into a string variable to use later. This is so that the program does not need to be changed if the download link is changed. The code I have so far is;
DatabaseReference fdb = FirebaseDatabase.getInstance().getReference();
//Query query = fdb.child("URLDOWNLOADS").child("WSSeasons")
fdb.addListenerForSingleValueEvent(new ValueEventListener()
{
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
for (DataSnapshot url : dataSnapshot.getChildren())
{
LoginInfoFirebasedb DownURL = url.getValue(LoginInfoFirebasedb.class);
String temp = DownURL.dwldURL;
URL1 = temp;
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError)
{
}
});
Some help would be seriously appreciated. I understand this may be a simple problem but I am stumped.
To get each url as a String, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("URLDOWNLOADS");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
String url = ds.child("URL").getValue(String.class);
Log.d(TAG, key + " / " + url);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
ref.addListenerForSingleValueEvent(valueEventListener);
Here is my database reference:
I want to read the two key's value(reminddate and title) and show on the listview.How can I do it?
Metodolist.java
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference hom = database.getReference("Student").child(student_id).child("event");
listview = findViewById(R.id.listview);
final ArrayAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,android.R.id.text1);
listview.setAdapter(adapter);
hom.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot event : dataSnapshot.getChildren() ) {
Event = event.getValue().toString();
Log.e("Metodolist",Event);
adapter.add(Event);
adapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Log.e("MeTo list","Pass1");
This is how can be done:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference eventRef = rootRef.child("Student").child(student_id).child("event");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String reminddate = ds.child("reminddate").getValue(String.class);
String title = ds.child("title").getValue(String.class);
Log.d("TAG", reminddate + " / " + title);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
eventRef.addListenerForSingleValueEvent(eventListener);
The output will be:
22-02-18 / A
23-02-18 / B