2017-06-05 47 views
0

我要檢查用戶輸入的代碼是否對數據庫代碼有效。如果是的話,它應該將firebase數據庫中的所有細節填充到佈局中的所有字段。否則,應該回到首頁給予用戶敬酒,他沒有被授權。無法正常使用意圖

問題是,即使用戶輸入的代碼與數據庫代碼相匹配,它也會帶我到HomePage。我該如何解決這個問題?任何人都可以指導我。

代碼在我onCreate方法:

inputCodesReference.addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
    for (DataSnapshot ds: dataSnapshot.getChildren()) { 
     enteredUserCode = (String) ds.child("code").getValue(); 
    } 
    } 
    @Override 
    public void onCancelled(DatabaseError databaseError) {} 
}); 

familyListReference.addListenerForSingleValueEvent(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
    for (DataSnapshot ds: dataSnapshot.getChildren()) { 
     String key = (String) ds.getKey(); 
     DatabaseReference keyReference = FirebaseDatabase.getInstance().getReference().child("familylist").child(key); 
     keyReference.addListenerForSingleValueEvent(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      final String code = dataSnapshot.child("code").getValue(String.class); 
      String family = dataSnapshot.child("family").getValue(String.class); 
      String father = dataSnapshot.child("fatherName").getValue(String.class); 
      String mother = dataSnapshot.child("motherName").getValue(String.class); 

      for (DataSnapshot childSnapshot: dataSnapshot.child("children").getChildren()) { 
      String child = childSnapshot.getValue(String.class); 
      if (code.equals(enteredUserCode)) 
       children.add(child); 
      } 

      if (code.equals(enteredUserCode)) { 
      Log.d(TAG, "[FINAL CHECK IN ACTIVITY] Database Code: " + code); 
      Log.d(TAG, "[FINAL CHECK IN ACTIVITY] User Input code: " + enteredUserCode); 
      familyName = family; 
      fatherName = father; 
      motherName = mother; 
      relativeLayout.setVisibility(View.VISIBLE); 
      welcomeMessage.setText("Welcome " + familyName + " family!"); 
      fatherRadioButton.setText(fatherName); 
      motherRadioButton.setText(motherName); 
      } else { 
      Intent intent = new Intent(getApplicationContext(), HomePage.class); 
      startActivity(intent); 
      finish(); 
      } 
     } 
     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      Log.d(TAG, "Read failed"); 
     } 
     }); // [Key Reference] 

    } // END of for Loop 
    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
    Log.d(TAG, "Read failed"); 
    } 
}); // [familyList Database Reference] 


//[CHECK IN] 
checkinButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     if (fatherRadioButton.isChecked()) { 
     checkIn = new CheckIn(familyName, fatherName, null, checkInTime, childrenToDatabase); 
     } else { 
     checkIn = new CheckIn(familyName, null, motherName, checkInTime, childrenToDatabase); 
     } 
     checkInCheckOutDatabaseReference.push().setValue(checkIn); 
     Intent intent = new Intent(CheckInFinalActivity.this, HomePage.class); 
     startActivity(intent); 
     Toast.makeText(CheckInFinalActivity.this, "Check in Successful!", Toast.LENGTH_LONG).show(); 
    } 
    } 
}); 
+0

你確定你不在其他地方調用'startActivity'嗎?此Firebase偵聽器是一個異步調用。添加更多的代碼部分將有助於診斷。 –

+0

檢查用戶的讀寫權限 – sivaprakash

+0

問題不在於讀寫。其關於將用戶帶到合適的意圖@siva – Chip

回答

0

你在哪裏得到每當DataSnapshotfirebase檢索爲這個調用在本質上是asynchronousenteredUserCodeonDataChange方法內部執行的線條。

因此,enteredUserCode可能暫時不可用於familyListReference。爲確保enteredUserCode可用,您需要在inputCodeReferenceonDataChange方法內移動familyListReference's監聽器。

這是修改後的代碼。

inputCodesReference.addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
    for (DataSnapshot ds: dataSnapshot.getChildren()) { 
     enteredUserCode = (String) ds.child("code").getValue(); 
    } 
    familyListReference.addListenerForSingleValueEvent(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
     for (DataSnapshot ds: dataSnapshot.getChildren()) { 
      String key = (String) ds.getKey(); 
      DatabaseReference keyReference = FirebaseDatabase.getInstance().getReference().child("familylist").child(key); 
      keyReference.addListenerForSingleValueEvent(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       final String code = dataSnapshot.child("code").getValue(String.class); 
       String family = dataSnapshot.child("family").getValue(String.class); 
       String father = dataSnapshot.child("fatherName").getValue(String.class); 
       String mother = dataSnapshot.child("motherName").getValue(String.class); 

       for (DataSnapshot childSnapshot: dataSnapshot.child("children").getChildren()) { 
       String child = childSnapshot.getValue(String.class); 
       if (code.equals(enteredUserCode)) 
        children.add(child); 
       } 

       if (code.equals(enteredUserCode)) { 
       Log.d(TAG, "[FINAL CHECK IN ACTIVITY] Database Code: " + code); 
       Log.d(TAG, "[FINAL CHECK IN ACTIVITY] User Input code: " + enteredUserCode); 
       familyName = family; 
       fatherName = father; 
       motherName = mother; 
       relativeLayout.setVisibility(View.VISIBLE); 
       welcomeMessage.setText("Welcome " + familyName + " family!"); 
       fatherRadioButton.setText(fatherName); 
       motherRadioButton.setText(motherName); 
       } else { 
       Intent intent = new Intent(getApplicationContext(), HomePage.class); 
       startActivity(intent); 
       finish(); 
       } 
      } 
      @Override 
      public void onCancelled(DatabaseError databaseError) { 
       Log.d(TAG, "Read failed"); 
      } 
      }); // [Key Reference] 

     } // END of for Loop 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
     Log.d(TAG, "Read failed"); 
     } 
    }); // [familyList Database Reference] 
    } 
    @Override 
    public void onCancelled(DatabaseError databaseError) {} 
}); 

//[CHECK IN] 
checkinButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     if (fatherRadioButton.isChecked()) { 
     checkIn = new CheckIn(familyName, fatherName, null, checkInTime, childrenToDatabase); 
     } else { 
     checkIn = new CheckIn(familyName, null, motherName, checkInTime, childrenToDatabase); 
     } 
     checkInCheckOutDatabaseReference.push().setValue(checkIn); 
     Intent intent = new Intent(CheckInFinalActivity.this, HomePage.class); 
     startActivity(intent); 
     Toast.makeText(CheckInFinalActivity.this, "Check in Successful!", Toast.LENGTH_LONG).show(); 
    } 
    } 
}); 

這確保enteredUserCode已準備好並可用於接下來的操作。

既然你有一種方法,代碼可以通過獲得rootFirebaseInstance而不是添加兩個偵聽器來進一步簡化。這取決於你如何組織你的json模式。

+0

這不適合我。當我添加這個,循環繼續,應用程序被吊死。 :( – Chip

+0

我稍微改了一下代碼,你用最新的那個試了一下嗎? –

+0

我可以通過github給你這個源代碼嗎?你可以查看一次,那會很棒。 – Chip