我試圖用Android應用程序實現Firebase,並且已經編寫了簡單的登錄代碼。問題是,除非在Activity類中使用Firebase代碼,否則用戶需要按登錄按鈕兩次。在另一個課程中使用Firebase,除了活動需要重複調用
簡而言之,用戶嘗試登錄,但Firebase引用不會即時檢索,因此會返回錯誤結果。但是,幾秒鐘後,您可以看到LogCat返回真實結果,並且如果用戶再次單擊登錄按鈕,則他/她能夠登錄。
logcat的
04-30 10:29:25.003: I/WMT(30932): activity
04-30 10:29:25.003: I/WMT(30932): controller
04-30 10:29:25.003: I/kolla(30932): https://REMOVEDURL.firebaseio.com/users/testing
04-30 10:29:25.003: I/kolla(30932): ITS TRUE!
04-30 10:29:25.133: D/dalvikvm(30932): GC_CONCURRENT freed 514K, 14% free 9799K/11271K, paused 6ms+13ms, total 62ms
04-30 10:29:26.274: I/kolla(30932): datachange
04-30 10:29:26.274: I/kolla(30932): https://REMOVEDURL.firebaseio.com/users/testing
04-30 10:29:26.274: I/kolla(30932): hej
04-30 10:29:26.274: I/kolla(30932): Normal password: hej
04-30 10:29:26.274: I/kolla(30932): Password is correct
04-30 10:29:26.274: I/kolla(30932): userExists is true
調用活動
public class MainActivity extends Activity {
private MainController mainController;
private EditText edtUsername;
private EditText edtPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainController = new MainController();
edtUsername = (EditText)findViewById(R.id.edtUsername);
edtPassword = (EditText)findViewById(R.id.edtPassword);
}
public void btnLoginClick(View view)
{
Log.i("WMT", "activity");
if(mainController.login(edtUsername.getText().toString(), edtP assword.getText().toString()) != null) {
Toast toast = Toast.makeText(getApplicationContext(), "Login correct, redirecting...", Toast.LENGTH_LONG);
toast.show();
User user = mainController.login(edtUsername.getText().toString(), edtPassword.getText().toString());
Intent intent = new Intent(this, UserWindowActivity.class);
intent.putExtra("user", user);
startActivity(intent);
} else {
Toast toast = Toast.makeText(getApplicationContext(), "Invalid username and/or password", Toast.LENGTH_SHORT);
toast.show();
}
}
}
控制器
public final class MainController {
// This controller handles main activity
private FirebaseDba dba;
public MainController() {
dba = new FirebaseDba();
}
public User login(String username, String password) {
Log.i("WMT", "controller");
return dba.login(username, password);
}
}
數據庫訪問類
public class FirebaseDba {
private final String FIREBASE_URL = //Removed for safety purposes;
private Firebase fb;
// String savedPassword;
boolean userExists;
public FirebaseDba() {
fb = new Firebase(FIREBASE_URL);
}
private Firebase getFirebaseConnection() {
return fb;
}
public User login(final String username, final String password) {
final Firebase dbaa = new Firebase(fb.toString() + "https://stackoverflow.com/users/" + username);
Log.i("kolla", dbaa.toString());
dbaa.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snap) {
Log.i("kolla" ,"datachange");
Log.i("kolla", dbaa.toString());
Object value = snap.getValue();
if (value != null) {
String dbPassword = snap.child("password").getValue().toString();
Log.i("kolla", dbPassword);
Log.i("kolla", "Normal password: " + password);
if (password.equals(dbPassword)) {
Log.i("kolla", "Password is correct");
changeToTrue();
if(userExists) Log.i("kolla", "userExists is true");
}
}
else {
userExists = false;
}
}
@Override
public void onCancelled(FirebaseError arg0) {
// TODO Auto-generated method stub
}
});
Log.i("kolla", "ITS TRUE!");
if (userExists == true) {
Log.i("kolla", "userExists equals true");
return new User(username, password);
}
//return new User(username, password);
return null;
}
public void changeToTrue() {
userExists = true;
}
}
編輯:有沒有人有這個問題?每個人都在他們的活動中運行他們的DB訪問嗎?因爲這聽起來很糟糕,我想用單獨的課程來解決這個問題。所有幫助讚賞。