我有问题,我无法获取数据值。.我不知道为什么..有人可以帮助我吗?
这是我的代码..我已经获得了大约2到3次的值..是什么让我的底层代码在日志中不起作用?嗯..我很困惑
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) {
}
});
}
}
我在这段代码中遇到了麻烦。无法获取该值:( 我想获得价值,因为我想减少我的粉末库存。.
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) {
}
});
}
}
答案 0 :(得分:1)
您似乎想查询POWDERS
属性具有特定值的powdername
下所有子节点的数据。
您可以在Firebase中执行以下操作:
mDatabase = FirebaseDatabase.getInstance().getReference("POWDERS");
mDatabase.orderByChild("powdername").equalTo(productname).addValueEventListener(new ValueEventListener() {
...
请花一些时间研究ordering and filtering data上的Firebase文档,因为它应该很好地解释了这种用例。
除此之外,我强烈建议您将数值作为实际数字存储在数据库中,因为这样可以避免在代码中进行容易出错的转换。