2017-02-22 19 views
1

本質上,我創建的鬧鐘僅允許您在掃描條碼的數值已輸入我的Firebase數據庫的子內容下的情況下關閉鬧鐘。目前我所擁有的掃描儀可掃描代碼並在Android應用程序的TextView contentTxt中顯示此值。如何檢查我的Firebase數據庫中的數據是否與我的Java中的TextView.toString相匹配?

當我點擊「checkButton」,它應該藉此TextView的改變它的toString,然後通過數據庫運行,以檢查是否有與之匹配的任何值。如果有,它會停止報警。如果沒有,則會顯示「這不是掃描的條形碼」通知。但是,即使我的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) { 

        } 
       }); 
      } 
     }); 

這裏是我的火力地堡的截圖還,如果這能幫助:

Screenshot showing my database's hierarchy and children

沒有實際的錯誤代碼或崩潰即將在我的代碼,它只是不斷說有沒有相同的值,然後我不能讓該死的警報關閉。

正如我的理解,我曾指出我的代碼給孩子「條形碼」,並通過其子「內容」命令,只要它是equalTo我contentTxt.toString。對我來說,這聽起來像是應該隔離那個價值,看看它是否有價值,所以也許你可以啓發我。我甚至嘗試將dataSnapshot.hasChildren()更改爲.exists()等,但無濟於事。

回答

1

只是爲了評論我解決了這個問題。我應該使用contentTxt.getText().toString()而不是contentTxt.toString()。愚蠢的錯誤,感謝您的幫助。

0

相反的dataSnapshot.hasChildren(),嘗試dataSnapshot.exists()

+0

我嘗試過,但不幸的是它仍然運行其他代碼。即使看看textview和db,字符串也完全一樣,所以我真的不知道我在做什麼錯了。 –

相關問題