gpt4 book ai didi

java - 如何从firebase数据库检索数据并将所有数据传递给sms

转载 作者:行者123 更新时间:2023-12-02 00:47:58 25 4
gpt4 key购买 nike

如何获取产品 Firebase 数据库下的所有数据,我只想获取产品名称和数量。图像中的日期是我的产品 ID,而不是实际日期。我的问题是我在产品表下只得到一个产品名称和数量。

enter image description here

到目前为止,这是我创建的代码

===这是我当前的代码======这是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();
}

最佳答案

如果您想获取基于您的结构的特定数字的所有产品名称和数量,您可以使用此

  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();
}




}
});

关于java - 如何从firebase数据库检索数据并将所有数据传递给sms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57882718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com