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
Related
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.
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 have a database that looks like this:
And my code like this:
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot uniqueKey:dataSnapshot.getChildren()) {
for (DataSnapshot keysiswa:uniqueKey.getChildren()) {
for (DataSnapshot valuesiswa:keysiswa.child("nama_siswa").getChildren()) {
String c = valuesiswa.getValue().toString();
Log.i(TAG,c);
}
}
}
}
I want to take the value of "nama_siswa" but nothing happens in logcat. What is wrong with my code?
Try this code.
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("siswa");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("Name:",dataSnapshot.getValue(User.class).name); // define your pojo class
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
To get the value of nama_siswa property, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference siswaRef = rootRef.child("siswa");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String nama_siswa = ds.child("nama_siswa").getValue(String.class);
Log.d("TAG", nama_siswa);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
siswaRef.addListenerForSingleValueEvent(valueEventListener);
The output will be:
chandra
coba
I am developing app to display user score that store in firebase database but after running application it won't display data in listview
below is my code:-
listViewLeaderBoard = (ListView) findViewById(R.id.listView_leader_board);
reference = FirebaseDatabase.getInstance().getReference("score");
leaderBoards = new ArrayList<>();
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
LeaderBoard leaderBoard = postSnapshot.getValue(LeaderBoard.class);
leaderBoards.add(0,leaderBoard);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Collections.sort(leaderBoards);
CustomLeaderBoardAdapter adapter = new CustomLeaderBoardAdapter(getApplicationContext(),R.layout.leader_board_list_layout,leaderBoards);
listViewLeaderBoard.setAdapter(adapter);
adapter.notifyDataSetChanged();
Database snapshot:-
Output:-
try this:
listViewLeaderBoard = (ListView) findViewById(R.id.listView_leader_board);
reference = FirebaseDatabase.getInstance().getReference("score");
leaderBoards = new ArrayList<>();
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
LeaderBoard leaderBoard = postSnapshot.getValue(LeaderBoard.class);
leaderBoards.add(leaderBoard);
CustomLeaderBoardAdapter adapter = new CustomLeaderBoardAdapter(getApplicationContext(),R.layout.leader_board_list_layout,leaderBoards);
listViewLeaderBoard.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
To solve this, move this line of code:
List<LeaderBoard> leaderBoards = new ArrayList<>();
inside onDataChange() method, otherwise it will always be, due the asynchronous behaviour of this method which is called even before you are trying to add those objects of LeaderBoard class to the list.
Also, leaderBoards.clear(); is no needed anymore. Your code should look something like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference scoreRef = rootRef.child("score");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String imageUrl = ds.child("imageUrl").getValue(String.class);
String name = ds.child("name").getValue(String.class);
long score = ds.child("score").getValue(Long.class);
Log.d("TAG", image + " / " + Urlname + " / " + score);
list.add(image + " / " + Urlname + " / " + score);
}
Log.d("TAG", list);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
scoreRef.addListenerForSingleValueEvent(eventListener);
You can find and change the rules for your database in the Firebase console.Simply choose your project, click on the Database section on the left, and then select the Rules tab.I don't know you have done this or not try this hope this will help you.
{
"rules": {
".read": true,
".write": true
}
}