【问题标题】:How to arrange firebase database items for each android user如何为每个android用户安排firebase数据库项目
【发布时间】:2018-10-16 20:57:12
【问题描述】:

我正在创建一个 android 应用程序来存储每个用户的姓名和手机号码,现在当第二个用户更新他的信息时,它正在删除存储在数据库中的第一个用户的信息,我有 2 个片段一个是登录片段(使用firebase auth) 和获取姓名和手机号码的第二个片段。如何分别为每个用户添加信息

这是我的登录代码

 btn_signIn = (Button) view.findViewById(R.id.sign_in_btn);
    btn_signIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String emailID = username.getText().toString();
            String paswd = password.getText().toString();
            if (emailID.isEmpty()) {
                username.setError("Provide your Email first!");
                username.requestFocus();
            } else if (paswd.isEmpty()) {
                password.setError("Set your password");
                password.requestFocus();
            }
            else if (emailID.isEmpty() && paswd.isEmpty()) {
                Toast.makeText(getActivity(), "Fields Empty!", Toast.LENGTH_SHORT).show();
            }
            else if (!(emailID.isEmpty() && paswd.isEmpty())) {


                mAuth.signInWithEmailAndPassword(emailID, paswd)
                        .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                // If sign in fails, display a message to the user. If sign in succeeds
                                // the auth state listener will be notified and logic to handle the
                                // signed in user can be handled in the listener.

                                if (!task.isSuccessful()) {
                                    // there was an error
                                    if (password.length() < 6) {
                                       // inputPassword.setError(getString(R.string.minimum_password));
                                    } else {
                                      //  Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                                    }
                                } else {
                                   Intent intent = new Intent(getActivity(), HomePageActivity.class);
                                    startActivity(intent);
                                   // finish();
                                }
                            }
                        });

.

            }







        }
    });

这是我在另一个片段中的 firebase 中存储手机号码的代码

 new AlertDialog.Builder(getActivity()).setTitle("Please Input Contact Information").setIcon(
                    android.R.drawable.ic_dialog_dialer).setView(
                    layout).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Dialog dialog = (Dialog) dialogInterface;
                    EditText inputMobile = (EditText) dialog.findViewById(R.id.dialog_et_mobile);
                    if (inputMobile.getText().toString().isEmpty()){
                        return;
                    }
                    try{
                        long number = Long.valueOf(inputMobile.getText().toString());
                        SPManipulation.getInstance(getActivity()).setMobile(inputMobile.getText().toString());
                        mTextMobile.setText(inputMobile.getText().toString());


                        String mobile = inputMobile.getText().toString();
                        DatabaseReference mynum = database.getReference("number");




                    }catch (Exception e){
                        Toast.makeText(getActivity(), "Please Input Correct Phone Number!", Toast.LENGTH_SHORT).show();
                    }
                }
            }).setNegativeButton("Cancel", null).show();
        }
    });

谁能帮助为每个用户分别存储信息,我搜索了很多,但没有任何帮助。可能是因为我是新手..请帮助我

【问题讨论】:

  • 使用他们的 auth.getcurentUser id 作为唯一键
  • 得到唯一键后我该怎么办
  • 也分享你的firebase数据库结构
  • 你能分享一下你存储数据的代码吗?
  • 使用 fcm id 作为父节点并在其中插入所有其他子节点。您将拥有单独的用户详细信息和节点

标签: android firebase firebase-realtime-database


【解决方案1】:

试试下面的代码 使用以下代码添加产品

DatabaseReference rootRef;
rootRef = FirebaseDatabase.getInstance().getReference();

 CommonLocationClass dataLocation = new CommonLocationClass();
            dataLocation.setLat(latLng.latitude);
            dataLocation.setLongi(latLng.longitude);
            dataLocation.setPhno("");
            dataLocation.setStatus(true);
            if (sessionManager.phNO() != null) {
                rootRef.child(sessionManager.phNO()).setValue(dataLocation);
            }

commonlocationclass 是一个具有 getter 和 setter 的模型类 .child(sessionManager.phNo()) 是您可以进一步识别项目的子名称

通过此代码,您可以更新字段

Map<String, Object> map = new HashMap<>();
            map.put("lat", latLng.latitude);
            map.put("longi", latLng.longitude);
            map.put("phno", sessionManager.phNO());
            map.put("status", true);
            if (sessionManager.phNO() != null)
                rootRef.child(sessionManager.phNO()).updateChildren(map);

试试这个代码,希望它能帮助你快乐编码!!!!!!

【讨论】:

    【解决方案2】:

    试试这个,

        new AlertDialog.Builder(getActivity()).setTitle("Please Input Contact Information").setIcon(
                        android.R.drawable.ic_dialog_dialer).setView(
                        layout).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Dialog dialog = (Dialog) dialogInterface;
                        EditText inputMobile = (EditText) dialog.findViewById(R.id.dialog_et_mobile);
                        if (inputMobile.getText().toString().isEmpty()){
                            return;
                        }
                        try{
                            long number = Long.valueOf(inputMobile.getText().toString());
                            SPManipulation.getInstance(getActivity()).setMobile(inputMobile.getText().toString());
                            mTextMobile.setText(inputMobile.getText().toString());
    
                FirebaseAuth   mAuth = FirebaseAuth.getInstance();
                String userID = mAuth.getCurrentUser().getUid();
    
                            String mobile = inputMobile.getText().toString();
                            DatabaseReference mynum = database.getReference().child(userID).child("number");
    
    
    
    
                        }catch (Exception e){
                            Toast.makeText(getActivity(), "Please Input Correct Phone Number!", Toast.LENGTH_SHORT).show();
                        }
                    }
                }).setNegativeButton("Cancel", null).show();
            }
        });
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      相关资源
      最近更新 更多