android studio上的firebase问题

时间:2018-02-15 11:55:10

标签: android firebase firebase-realtime-database

我目前正在为Android编写博客应用程序。但我的firebase数据库有问题。我想将TextInputLayout中的用户输入添加到firebase数据库。

当用户创建帐户时,数据库中的“名称”字段就是:

  

android.support.design.widget.TextInputEditText {1785539 ​​VFED..CL。   ........ 0,0-966,118}

我不知道为什么,我花了2天时间来解决问题 - 但不知道。 其他一切都很好。

mToolbar = (Toolbar) findViewById(R.id.register_toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle("New Account");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mRegProgress = new ProgressDialog(this);

    mAuth = FirebaseAuth.getInstance();

    mDisplayName = (TextInputLayout) findViewById(R.id.reg_display_name);
    mEmail = (TextInputLayout) findViewById(R.id.reg_email);
    mPassword = (TextInputLayout) findViewById(R.id.reg_password);
    mCreateBtn = (Button) findViewById(R.id.reg_create_btn);

    mCreateBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String display_name = mDisplayName.getEditText().toString();
            String email = mEmail.getEditText().getText().toString();
            String password = mPassword.getEditText().getText().toString();

            if (!TextUtils.isEmpty(display_name) || !TextUtils.isEmpty(email) || !TextUtils.isEmpty(password)) {

                mRegProgress.setTitle("creating user");
                mRegProgress.setMessage("please wait...");
                mRegProgress.setCanceledOnTouchOutside(false);
                mRegProgress.show();

                register_user(display_name, email, password);
            }
        }
    });
}

private void register_user(final String display_name, String email, String password) {

            mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    if (task.isSuccessful()) {

                        FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();

                        String uid = current_user.getUid();

                        mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(uid);

                        HashMap<String, String> userMap = new HashMap<>();
                        userMap.put("name", display_name);
                        userMap.put("status", "Hey and welcome");
                        userMap.put("image", "default");
                        userMap.put("thumb_image", "default");

                        mDatabase.setValue(userMap);

希望有人可以提供帮助。

最好的问候 - 限制

3 个答案:

答案 0 :(得分:0)

EditText获取文本/字符串使用此:

editText.getText().toString();

答案 1 :(得分:0)

要获取文本,只需使用以下代码行:

String display_name = mDisplayName.getText().toString();

答案 2 :(得分:0)

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

    mRegProgress = new ProgressDialog(this);

    mAuth = FirebaseAuth.getInstance();

    mDisplayName = findViewById(R.id.reg_display_name);
    mEmail = findViewById(R.id.reg_email);
    mPassword = findViewById(R.id.reg_password);
    mCreateBtn = findViewById(R.id.reg_create_btn);

    mCreateBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String display_name = mDisplayName.getEditText().toString();
            String email = mEmail.getEditText().getText().toString();
            String password = mPassword.getEditText().getText().toString();

            if (!TextUtils.isEmpty(display_name) || !TextUtils.isEmpty(email) || !TextUtils.isEmpty(password)) {

                mRegProgress.setTitle("creating user");
                mRegProgress.setMessage("please wait...");
                mRegProgress.setCanceledOnTouchOutside(false);
                mRegProgress.show();

                register_user(display_name, email, password);
            }
        }
    });
}

private void register_user(String display_name, String email, String password) {

    mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

            if (task.isSuccessful()) {

                mRegProgress.dismiss();

                Intent ramaIntent = new Intent(RegisterActivity.this, MainActivity.class);
                startActivity(ramaIntent);
                finish();

            } else {

                mRegProgress.hide();
                Toast.makeText(RegisterActivity.this, "Cannot sign in. Please check the details and try again.", Toast.LENGTH_LONG).show();

            }
        }
    });
}

希望这有助于某人!