我要檢查用戶輸入的代碼是否對數據庫代碼有效。如果是的話,它應該將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();
}
}
});
你確定你不在其他地方調用'startActivity'嗎?此Firebase偵聽器是一個異步調用。添加更多的代碼部分將有助於診斷。 –
檢查用戶的讀寫權限 – sivaprakash
問題不在於讀寫。其關於將用戶帶到合適的意圖@siva – Chip