[英]How to retrieve data from firebase database and pass all the data to sms
我如何獲取我的Products Firebase數據庫下的所有數據,我只想獲取productName和數量。 圖片中的日期是我的productID,而不是實際日期。 我的問題是我在“產品”表下僅獲得一個productName和數量。
到目前為止,這是我創建的代碼
===這是我當前的代碼=== ===這是onclick方法===
DatabseReference reff = FirebaseDatabase.getInstance().getReference("Cart List");
DatabseReference reff2= FirebaseDatabase.getInstance().getReference("Cart List");
reff.child("User View").child(phoneNumber).child("Products").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snap: dataSnapshot.getChildren()){
//first get all the dates
String getDates = snap.getKey();
//add all the dates to reff2
reff2.child("User View").child(phoneNumber).child("Products").child.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snap1: dataSnapshot.getChildren()){
//value of product
String myProductName = snap1.child("ProductName").getValue(String.class);
//value of quanity just convert it to int if you want to calculate
String myQuantity= snap1.child("quantity").getValue(String.class);
//add your method
setSMSData(myProductName,myQuantity);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
===這是setSMSData ====
private void setSMSData (String myProductName, String myQuantity) {
// add the phone number in the data
Uri uri = Uri.parse("smsto:" + "09257777547");
Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
// add the message at the sms_body extra field
smsSIntent.putExtra("sms_body", "Order "+ myProductName +":"+ myQuantity +" (Sent Via SKIP MOBILE)");
try{
startActivity(smsSIntent);
} catch (Exception ex) {
Toast.makeText(CartActivity.this, "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
如果要根據結構在特定編號上獲取所有productName and quantity
,可以使用此功能
DatabaseReference reff = FirebaseDatabase.getInstance().getReference("Cart List");
final DatabaseReference reff2= FirebaseDatabase.getInstance().getReference("Cart List");
reff.child("user View").child("09553706928").child("Products").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snap: dataSnapshot.getChildren()){
String getDate = snap.getKey();
reff2.child("user View").child("09553706928").child("Products").child(getDate).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snap1: dataSnapshot.getChildren()){
String myProductName = snap1.child("ProductName").getValue(String.class);
Toast.makeText(getApplicationContext(),myProductName,Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
並在您的方法setSMSData()
private void setSMSData(String productName,String quantity) {
// add the phone number in the data
Uri uri = Uri.parse("smsto:" + "09257777547");
Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
// add the message at the sms_body extra field
smsSIntent.putExtra("sms_body", "Order "+ productName+":"+quantity+" (Sent Via
SKIP MOBILE)");
try{
startActivity(smsSIntent);
} catch (Exception ex) {
Toast.makeText(CartActivity.this, "Your sms has
failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.