【问题标题】:How to check if the data in my Firebase database matches my TextView.toString in Java?如何检查我的 Firebase 数据库中的数据是否与 Java 中的 TextView.toString 匹配?
【发布时间】: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


    【解决方案1】:

    只是为了评论我解决了这个问题。我应该使用contentTxt.getText().toString() 而不是contentTxt.toString()。愚蠢的错误,感谢您的帮助。

    【讨论】:

      【解决方案2】:

      试试dataSnapshot.exists(),而不是dataSnapshot.hasChildren()

      【讨论】:

      • 我也试过了,但不幸的是它仍然运行 else 代码。即使查看 textview 然后查看 db,字符串完全相同,所以我真的不知道我在做什么错。
      猜你喜欢
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-25
      相关资源
      最近更新 更多