简体   繁体   中英

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. the date in the image is my productID not actual date. my problem is i only get one productName and quantity in under the Product table.

在此处输入图片说明

so far this is the code i created

=== This is my current code === === this is the onclick method ===

   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 ====

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

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

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




    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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