要解决“Android读取Firebase复选框行为怪异”的问题,可以尝试以下解决方法:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference checkboxRef = database.getReference("checkbox");
checkboxRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
boolean isChecked = dataSnapshot.getValue(Boolean.class);
// 更新复选框状态
checkbox.setChecked(isChecked);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "Failed to read checkbox value.", databaseError.toException());
}
});
checkboxRef.setValue(checkbox.isChecked());
checkboxRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
boolean isChecked = dataSnapshot.getValue(Boolean.class);
// 更新复选框状态
checkbox.setChecked(isChecked);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "Failed to read checkbox value.", databaseError.toException());
}
});
{
"rules": {
"checkbox": {
".read": true,
".write": true
}
}
}
通过尝试以上解决方法,您应该能够解决“Android读取Firebase复选框行为怪异”的问题。