简体   繁体   English

如何从Firebase数据库检索数据并将所有数据传递到短信

[英]How to retrieve data from firebase database and pass all the data to sms

How can i get all the data under my Products Firebase database, i only want to get the productName and quantity. 我如何获取我的Products Firebase数据库下的所有数据,我只想获取productName和数量。 the date in the image is my productID not actual date. 图片中的日期是我的productID,而不是实际日期。 my problem is i only get one productName and quantity in under the Product table. 我的问题是我在“产品”表下仅获得一个productName和数量。

在此处输入图片说明

so far this is the code i created 到目前为止,这是我创建的代码

=== This is my current code === === this is the onclick method === ===这是我当前的代码=== ===这是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) {

 }
 });

=== THis is the setSMSData ==== ===这是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();
                }

IF you want to get all the productName and quantity on a specific number base on your structure you can use this 如果要根据结构在特定编号上获取所有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) {

        }
    });

And in your method setSMSData() 并在您的方法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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM