简体   繁体   中英

How to get value repeatedly from a Firebase Realtime Database reference?

I have a problem, i cant get my data values.. i dont know why.. can anybody help me please??

and this is my code.. i already get values maybe 2 until 3 times.. whats make my bottom code doesnt work in log? huh.. i'm so confused

public class transactioncheckout_page extends AppCompatActivity {

    private DatabaseReference mDatabase;
    private DatabaseReference dDatabase;

    TextView txtProductName,txtTotalProduct,txtPaymentMethod,txtProductPrice,txtValueOfSubtotal, txtValueOfTotal;
    String productprice;
    int productpriceint;
    Button btConfirmTransaction;
    int valueofsubtotal,powqtyint;
    String powcode, powqty;


    long id = 0;
    transactions trans;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transactioncheckout_page);

        Intent i = getIntent();

        final String productname = i.getStringExtra("productname");
        final String totalproduct = i.getStringExtra("totalproduct");
        String paymentmethod = i.getStringExtra("paymentmethod");


        txtProductName = findViewById(R.id.txtProductName);
        txtProductName.setText(productname);

        txtTotalProduct = findViewById(R.id.txtTotalProduct);
        txtTotalProduct.setText(totalproduct+"X");

        txtPaymentMethod = findViewById(R.id.txtPaymentMethodValue);
        txtPaymentMethod.setText(paymentmethod);

        txtProductPrice = findViewById(R.id.txtProductPrice);
        txtValueOfSubtotal = findViewById(R.id.txtValueOfSubtotal);
        txtValueOfTotal = findViewById(R.id.txtValueOfTotal);

        mDatabase = FirebaseDatabase.getInstance().getReference();
        mDatabase.child("PRODUCTS").orderByChild("productname").equalTo(productname).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    productprice = ds.child("productprice").getValue(String.class);
                    powcode = ds.child("powdercode").getValue(String.class);
                    System.out.println("POWDER CODE IS " + powcode);
                    System.out.println("the value of price is " + productprice);
                    txtProductPrice.setText("(@Rp."+ productprice + ")");


                    productpriceint = Integer.parseInt(productprice);

                    valueofsubtotal = Integer.parseInt(totalproduct) * productpriceint;

                    txtValueOfSubtotal.setText("Rp. " + Integer.toString(valueofsubtotal));
                    txtValueOfTotal.setText("Rp. " + Integer.toString(valueofsubtotal));

                }
            }


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

            }
        });

        trans = new transactions();
        mDatabase = FirebaseDatabase.getInstance().getReference().child("TRANSACTIONS");
        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists())
                    id = (dataSnapshot.getChildrenCount());

            }

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

            }
        });

        btConfirmTransaction = findViewById(R.id.btConfirmTransaction);
        btConfirmTransaction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String pname = txtProductName.getText().toString();
                final String sumofp = totalproduct;
                final String totalprice = Integer.toString(valueofsubtotal);

                String currentuser = FirebaseAuth.getInstance().getCurrentUser().getEmail();
                final String userid = currentuser;

                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                Date ddate = new Date();
                String strdate = dateFormat.format(ddate).toString();
                final String date = strdate;

                final String ppaymentmethod = txtPaymentMethod.getText().toString();


                trans.setProductname(pname);
                trans.setSumofproduct(sumofp);
                trans.setTotalprice(totalprice);
                trans.setPaymentmethod(ppaymentmethod);
                trans.setPowdercode(powcode);
                System.out.println("Powder code = " + powcode);
                trans.setUser(userid);
                trans.setDate(date);

                mDatabase.child("TRANS" + (id+1)).setValue(trans);

                Toast.makeText(transactioncheckout_page.this, "Data inserted", Toast.LENGTH_LONG).show();


            }
        });

        mDatabase = FirebaseDatabase.getInstance().getReference("POWDERS");
        System.out.println("ABC0000000000000000000000000000000000000000000000000000000");
        mDatabase.child("powdername").equalTo(productname).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    powqty = ds.child("powderquantity").getValue(String.class);
//                            powqtyint = Integer.parseInt(powqty);
//                            powqtyint = powqtyint - (Integer.parseInt(sumofp)*100);
//                            powqty = Integer.toString(powqtyint);

                    System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + powqty);

                }

            }

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

            }
        });





    }
}

I have troubled in this code.. cant get the value :( i want to get value because i want to minus my powders stock..

mDatabase = FirebaseDatabase.getInstance().getReference("POWDERS");
        System.out.println("ABC0000000000000000000000000000000000000000000000000000000");
        mDatabase.child("powdername").equalTo(productname).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    powqty = ds.child("powderquantity").getValue(String.class);
//                            powqtyint = Integer.parseInt(powqty);
//                            powqtyint = powqtyint - (Integer.parseInt(sumofp)*100);
//                            powqty = Integer.toString(powqtyint);

                    System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + powqty);

                }

            }

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

            }
        });
}
}

this is my database pic 点击这里查看我的数据库结构

You seem to want to query the data for all child nodes under POWDERS where the powdername property has a certain value.

You do that in Firebase with:

mDatabase = FirebaseDatabase.getInstance().getReference("POWDERS");
mDatabase.orderByChild("powdername").equalTo(productname).addValueEventListener(new ValueEventListener() {
    ...

Please spend some time studying the Firebase documentation on ordering and filtering data , as it should explain this type of use-case pretty well.

Aside from that, I highly recommend storing your numeric values as actual numbers in the database, as it prevents having to do error-prone conversions in your code.

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