如何将数据从firebase数据库传递到短信?

我正在从我的 firebase 数据库获取数据,但是当我将其传递给参数并使用它在短信中写入时,它只能在短信中获取一个数据,但是当我使用 toast 时,它会显示所有数据,但是当我传递给短信时,我只得到一个?我的代码有什么问题吗?我如何显示我在短信功能中检索到的所有数据,下面是我的代码和我的 firebaseddatabase 的屏幕截图,我想显示所有产品名称和数量。

感谢您的帮助

基于Fire的数据库示例

=== smsSendToBtn(此代码用于从 Firebased 数据库检索数据)===

 smsSendToBtn.setOnClickListener(new View.OnClickListener() {

        //private String Cart;


        public void onClick(View view) {


            DatabaseReference reff = FirebaseDatabase.getInstance().getReference("Cart List");

            final DatabaseReference reff2= FirebaseDatabase.getInstance().getReference("Cart List");


            reff.child("User View").child(Prevalent.CurrentOnlineUsers.getPhone()).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(Prevalent.CurrentOnlineUsers.getPhone()).child("Products").addValueEventListener(new ValueEventListener() {

                            @Override

                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                                for(DataSnapshot snap1: dataSnapshot.getChildren()){


                                    String myProductName = snap1.child("productName").getValue(String.class);

                                    String myQuantity = snap1.child("quantity").getValue(String.class);


                                    Toast.makeText(getApplicationContext(),myProductName + " " + myQuantity,Toast.LENGTH_SHORT).show();

                                    setSMSData(myProductName,myQuantity);



                                }


                            }


慕标5832272
浏览 106回答 1
1回答

森林海

这是一个例子List<String> prods = new ArrayList<>();List<String> quantity = new ArrayList<>();smsSendToBtn.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; //private String Cart;&nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; DatabaseReference reff = FirebaseDatabase.getInstance().getReference("Cart List");&nbsp; &nbsp; &nbsp; &nbsp; final DatabaseReference reff2= FirebaseDatabase.getInstance().getReference("Cart List");&nbsp; &nbsp; &nbsp; &nbsp; reff.child("User View").child(Prevalent.CurrentOnlineUsers.getPhone()).child("Products").addValueEventListener(new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(@NonNull DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(DataSnapshot snap: dataSnapshot.getChildren()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String getDate = snap.getKey();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reff2.child("User View").child(Prevalent.CurrentOnlineUsers.getPhone()).child("Products").addValueEventListener(new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(@NonNull DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(DataSnapshot snap1: dataSnapshot.getChildren()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String myProductName = snap1.child("productName").getValue(String.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String myQuantity = snap1.child("quantity").getValue(String.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prods.Add(myProductName );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quantity.Add(myQuantity);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getApplicationContext(),myProductName + " " + myQuantity,Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setSMSData(prods,quantity);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(@NonNull DatabaseError databaseError) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(@NonNull DatabaseError databaseError) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }设置短信数据&nbsp;private void&nbsp; setSMSData(List<String> products,List<String> quantitys) {&nbsp; &nbsp; &nbsp; &nbsp; // add the phone number in the data&nbsp; &nbsp; &nbsp; &nbsp; Uri uri = Uri.parse("smsto:" + "09257777547");&nbsp; &nbsp; &nbsp; &nbsp; Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);&nbsp; &nbsp; &nbsp; &nbsp; // add the message at the sms_body extra field&nbsp; &nbsp; &nbsp; &nbsp; List<String> format = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; int i=-1;&nbsp; &nbsp; &nbsp; &nbsp; foreach(String str in products){&nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; format.Add(str+":"+quantitys[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; String formats= Arrays.toString(format);&nbsp; &nbsp; &nbsp; &nbsp; smsSIntent.putExtra("sms_body", "Order "+formats+" (Sent Via SKIP MOBILE)");&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(smsSIntent);&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(CartActivity.this, "Your sms has failed...",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java