【发布时间】:2017-02-22 06:15:20
【问题描述】:
本质上,我正在创建一个闹钟,仅当您扫描其值已在子“内容”下输入到我的 Firebase 数据库中的条形码时,才允许您关闭闹钟。我目前拥有的是一个扫描仪,它扫描代码并将这个值显示在 Android 应用程序中的 TextView contentTxt 上。
当我单击“checkButton”时,它应该将此 TextView 更改为 String,然后运行数据库以检查是否有任何与其匹配的值。如果有,它将停止警报。如果没有,它将显示“这不是扫描的条形码”的通知。但是,即使我的 firebase 中的值与我的 TextView 中的值相同,它也只会说它不是扫描的条形码。
这是我的代码:
final Button checkButton = (Button) findViewById(R.id.checkBtn);
checkButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mDatabase.child("users").child(mUserId).child("barcodes").orderByChild("content")
.equalTo(contentTxt.toString()).addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren()){
// method that alters update text TextBox
set_alarm_text("Alarm off!");
// cancel the alarm
alarm_manager.cancel(pending_intent);
// put in extra string into my_intent, telling the clock that "Alarm Off" was pressed
my_intent.putExtra("extra", "alarm off");
// stop the ringtone
sendBroadcast(my_intent);
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"This is not a scanned barcode!", Toast.LENGTH_SHORT);
toast.show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
如果有帮助,这也是我的 Firebase 的屏幕截图:
我的代码中没有出现实际的错误代码或崩溃,它只是不断地说没有相同的值,然后我无法关闭该死的警报。
据我了解,我已将我的代码指向子“条形码”并按其子“内容”排序,前提是它与我的 contentTxt.toString 相等。对我来说,这听起来应该隔离那个值,看看它是否有任何价值,所以也许你可以启发我。我什至尝试尝试将 dataSnapshot.hasChildren() 更改为 .exists() 等,但无济于事。
【问题讨论】:
标签: android database firebase firebase-realtime-database match