没什么可救的

时间:2019-06-18 09:53:54

标签: java android firebase

我对使用android studio制作应用程序很陌生,我建议您遵循我做的教程。一切都很好,直到我运行该程序为止,由于某种原因,什么都不会保存到firebase。很抱歉,这是一个简单的问题,但是就像我说的那样,我对Java和应用程序编程都是全新的。

我尝试逐行浏览代码,并将其与教程制作者的代码进行比较。

    package com.hossain.mylearningproject;

    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;

    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.os.Bundle;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    import com.google.android.gms.tasks.OnCompleteListener;
    import com.google.android.gms.tasks.Task;
    import com.google.firebase.database.DataSnapshot;
    import com.google.firebase.database.DatabaseError;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.database.ValueEventListener;

    import java.util.HashMap;

    public class RegisterActivity extends AppCompatActivity {

    private Button CreateAccountBtn;
    private EditText Inputname, InputPhoneNumber, InputPassword;
    private ProgressDialog LoadingBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        CreateAccountBtn = (Button) findViewById(R. id. register_btn);
        Inputname = (EditText) findViewById(R. id. register_username_input);
        InputPhoneNumber = (EditText) findViewById(R. id. register_phone_number_input);
        InputPassword = (EditText) findViewById(R. id. register_password_input);
        LoadingBar = (ProgressDialog) new ProgressDialog(this);

        CreateAccountBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CreateAccount();
            }
        });
    }


    private void CreateAccount(){
        String name = Inputname.getText().toString();
        String phonenumber = InputPhoneNumber.getText().toString();
        String password = InputPassword.getText().toString();

        if (TextUtils.isEmpty(name)){
            Toast.makeText(this, "Please Write you're name...", Toast.LENGTH_SHORT).show();
        }
        else if (TextUtils.isEmpty(phonenumber)){
            Toast.makeText(this, "Please Write you're phone number...", Toast.LENGTH_SHORT).show();
        }
        else if (TextUtils.isEmpty(password)){
            Toast.makeText(this, "Please Write you're Password...", Toast.LENGTH_SHORT).show();
        }
        else{
            LoadingBar.setTitle("Create account");
            LoadingBar.setMessage("Please wait while we check the credentialls.");
            LoadingBar.setCanceledOnTouchOutside(false);
            LoadingBar.show();

            ValidateCredentialls(name, phonenumber);
        }
    }

    private void ValidateCredentialls(final String name, final String phonenumber){
        final DatabaseReference Rootref;
        Rootref = FirebaseDatabase.getInstance().getReference();
        Rootref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (!(dataSnapshot.child("Users").child(phonenumber).exists())){
                    HashMap<String, Object> Userdatamap = new HashMap<>();
                            Userdatamap.put("phone number", phonenumber);
                            Userdatamap.put("Username", name);
                            Userdatamap.put("password", InputPassword);

                            Rootref.child("Users").child(phonenumber).updateChildren(Userdatamap)
                                    .addOnCompleteListener(new OnCompleteListener<Void>()
                                    {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task)
                                        {
                                            if(task.isSuccessful())
                                            {
                                                Toast.makeText(RegisterActivity.this, "Congratulations, you're account has now been created", Toast.LENGTH_SHORT).show();
                                                LoadingBar.dismiss();

                                                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                                startActivity(intent);

                                            }
                                            else
                                                {
                                                LoadingBar.dismiss();
                                                Toast.makeText(RegisterActivity.this, "Connection problem, Please check you're internet connection.", Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
                }
                else
                    {
                    Toast.makeText(RegisterActivity.this, "An account with " + phonenumber + " already exists", Toast.LENGTH_SHORT).show();
                    LoadingBar.dismiss();
                    Toast.makeText(RegisterActivity.this, "Please try again using a diffrent phone number", Toast.LENGTH_SHORT).show();

                    Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                    startActivity(intent);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}

我希望它可以使用不同的“子代”保存到Firebase,但是什么也没有保存

我已经将其发布到了SuperUser,但是在查看其他问题时,我意识到某人实际做出回应的几率很低:/,我也有一些很好的经验,可以在这里快速,轻松地获得答案。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,然后我才知道它仅仅是因为这个规则,所以我无法在数据库中写任何东西。Check if write rule is set to true . Because it is used to describes if and when data is allowed to be written.创建一个数据模型,说

data class User(
var email: String? = "",
var name: String? = "",
var phoneNumber:String? =""
)

现在要写入Firebase数据库,您要做的就是创建一个Firebase数据库实例

 database = FirebaseDatabase.getInstance().getReference("users")

然后将值添加到模型中并将其传递到数据库

 val user = User("abc.gmail.com, "abc",
        "858689849")
    database.child(userId).setValue(user)

以您为例。

  private void ValidateCredentialls(final String name, final String phonenumber){
    final DatabaseReference Rootref;
    Rootref = FirebaseDatabase.getInstance().getReference("users")
   ;
    User user =new  User("abc.gmail.com, "abc",
    "858689849");
database.child(userId).setValue(user)
}