如何在Android中将两个双精度数乘以两倍?

时间:2018-12-05 18:49:24

标签: android

我想将两个加倍的值相乘并显示其结果。此处c1 = 10.0,dis或condition1 = 1.82,inc = 4.00,inac == 15.00。我检查了吐司中的每个变量。每个变量都在赋予价值。我认为问题在于乘法方法。

 db=FirebaseDatabase.getInstance().getReference().child("ServiceConsumers").child("distance");
        db.child(currentUser).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                dis=dataSnapshot.child("distance").getValue().toString();
                Toast.makeText(getApplicationContext(), "dis" + dis,Toast.LENGTH_LONG).show();


                conditn1=Double.valueOf(dis);
                Toast.makeText(getApplicationContext(), "conditn1" +conditn1,Toast.LENGTH_LONG).show();


            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
        if (conditn1 >= c1){
            double total= conditn1 * inac;
            Toast.makeText(getApplicationContext(), "pay" + total, Toast.LENGTH_LONG).show();

            paymnt.setText(String.valueOf(total));
        } else {
            if (conditn1 <= c1){
            double mult=conditn1*inc;
                //String tot=String.valueOf(conditn1 * inc);
                Toast.makeText(getApplicationContext(), "payi = " + mult, Toast.LENGTH_LONG).show();
                paymnt.setText(String.valueOf(mult));

            }
        }

        rate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(), RateProvider.class);
                intent.putExtra("prvdrName", value3);
                startActivity(intent);
            }
        });


    }

1 个答案:

答案 0 :(得分:1)

供您参考,Firebase的几乎每个功能都是异步功能。
所以您应该在 onDataChange 方法中进行计算。
所以您的代码将是

db = FirebaseDatabase.getInstance().getReference().child("ServiceConsumers").child("distance");
db.child(currentUser).addValueEventListener(new ValueEventListener() {
 @Override
 public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

  dis = dataSnapshot.child("distance").getValue().toString();
  Toast.makeText(getApplicationContext(), "dis" + dis, Toast.LENGTH_LONG).show();


  conditn1 = Double.valueOf(dis);
  Toast.makeText(getApplicationContext(), "conditn1" + conditn1, Toast.LENGTH_LONG).show();

  if (conditn1 >= c1) {
   double total = conditn1 * inac;
   Toast.makeText(getApplicationContext(), "pay" + total, Toast.LENGTH_LONG).show();

   paymnt.setText(String.valueOf(total));
  } else {
   if (conditn1 <= c1) {
    double mult = conditn1 * inc;
    //String tot=String.valueOf(conditn1 * inc);
    Toast.makeText(getApplicationContext(), "payi = " + mult, Toast.LENGTH_LONG).show();
    paymnt.setText(String.valueOf(mult));

   }
  }
 }

 @Override
 public void onCancelled(@NonNull DatabaseError databaseError) {

 }
});