iT邦幫忙

2021 iThome 鐵人賽

DAY 26
0
自我挑戰組

創業歷程系列 第 26

企劃實現(26)

  • 分享至 

  • xImage
  •  

在firebase製作登入系統
可以使用myRef.child("member").get().addOnCompleteListener進入資料庫裡
task.getResult().getChildrenCount()可以偵測有幾筆資料
使用

 myRef.child("member").get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DataSnapshot> task) {
                Boolean ok = true;
                long a =task.getResult().getChildrenCount();
                if(account.getText().toString().matches("") || password.getText().toString().matches("")) {
                    Toast.makeText(login.this, "請輸入帳號密碼", Toast.LENGTH_SHORT).show();
                }
                else {
                    for (int i = 1; i <= a; i++) {
                        if (account.getText().toString().equals(task.getResult().child(String.valueOf(i)).child("account").getValue()) & password.getText().toString().equals(task.getResult().child(String.valueOf(i)).child("password").getValue())) {
                            ok = false;
                            SharedPreferences member_pref = getSharedPreferences("member", MODE_PRIVATE);
                            member_pref.edit().putString("USER", account.getText().toString())
                                    .commit();
                            Intent intent = new Intent(login.this, MainActivity.class);
                            startActivity(intent);
                        }
                    }
                    if (ok){
                        Toast.makeText(login.this,"帳密錯誤",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

上一篇
企劃實現(25)
下一篇
企劃實現(27)
系列文
創業歷程30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言