我正在为“提交”按钮设置计数器,以对Firebase数据库中的产品数量进行计数,应该将数量增加+1
我已经尝试过交易,并且行得通。
我需要检索存储在数据库中的最后一个值。
public class MainActivity extends AppCompatActivity {
Button submit, fetch;
DatabaseReference rootRef, demoRef;
TextView demoValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
demoValue = findViewById(R.id.tvValue);
submit = findViewById(R.id.btnSubmit);
fetch = findViewById(R.id.btnFetch);
//database reference pointing to root of database
rootRef = FirebaseDatabase.getInstance().getReference().child("demo").child("Products");
demoRef=rootRef.child("quantity");
//database reference pointing to demo node
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
demoRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Long value = mutableData.getValue(Long.class);
if (value == null) {
mutableData.setValue(0);
}
else {
mutableData.setValue(value - 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
}
});
}
});
fetch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rootRef.child("quantity").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Integer value = dataSnapshot.getValue(Integer.class);
demoValue.setText(value);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
}
当我按下获取按钮以显示存储在数据库中的值时,该应用程序什么也不做或崩溃!
我的Logcat
android.content.res.Resources$NotFoundException: String resource ID #0xa2
at android.content.res.Resources.getText(Resources.java:472)
at android.widget.TextView.setText(TextView.java:4465)
at com.example.ahmed.firebaseupdatequatity.MainActivity$2$1.onDataChange(MainActivity.java:95)
at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@16.0.5:183)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.5:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.5:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.5:55)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
在这里检查我的数据库结构 enter image description here
答案 0 :(得分:1)
您遇到以下错误:
android.content.res.Resources$NotFoundException: String resource ID #0xa2
因为您正尝试将整数而不是字符串传递给setText()
方法。要解决此问题,请更改以下代码行:
demoValue.setText(value);
到
demoValue.setText(String.valueOf(value));
或
demoValue.setText("" + value);