java - 输入已更新到数据库中的错误字段

标签 java android firebase google-cloud-firestore

我已经完成编码,我想更新个人资料。但用户输入的输入被放置在数据库内的错误字段中。我不知道什么时候出错了。

性别显示电话号码,电话号码显示“性别” 如何解决这个问题?

这是更新功能。

private void showUpdateDialog(String phoneNumber) {


        //init dialog
        bottomSheetDialog = new BottomSheetDialog(this);
        bottomSheetDialog.setTitle("one more step!");
        bottomSheetDialog.setCanceledOnTouchOutside(false);
        bottomSheetDialog.setCancelable(false);
        View sheetView = getLayoutInflater().inflate(R.layout.layout_update_information, null);

        Button btn_update = sheetView.findViewById(R.id.btn_update);
        TextInputEditText edt_name = sheetView.findViewById(R.id.edt_name);
        TextInputEditText edt_email = sheetView.findViewById(R.id.edt_email);
        TextInputEditText edt_address = sheetView.findViewById(R.id.edt_address);
        TextInputEditText edt_gender = sheetView.findViewById(R.id.edt_gender);

        btn_update.setOnClickListener(view -> {

            if (!dialog.isShowing())
                dialog.dismiss();

            User user = new User(edt_name.getText().toString(),
                    edt_email.getText().toString(),
                    edt_address.getText().toString(),
                    edt_gender.getText().toString(),
                    phoneNumber);
            userRef.document(phoneNumber)
                    .set(user)
                    .addOnSuccessListener(aVoid -> {
                        bottomSheetDialog.dismiss();
                        if (dialog.isShowing())
                            dialog.dismiss();
                        Toast.makeText(HomeActivity.this, " Thank You", Toast.LENGTH_SHORT).show();
                    }).addOnFailureListener(e -> {
                if (dialog.isShowing())
                    dialog.dismiss();
                bottomSheetDialog.dismiss();
                Toast.makeText(HomeActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
            });

        });

        bottomSheetDialog.setContentView(sheetView);
        bottomSheetDialog.show();

    }

更新布局信息

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical"
    android:padding="8dp">



    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edt_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="Your name" />
    </com.google.android.material.textfield.TextInputLayout>



    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edt_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="Your email" />
    </com.google.android.material.textfield.TextInputLayout>


    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edt_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="Your address" />
    </com.google.android.material.textfield.TextInputLayout>


    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edt_gender"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="Your gender" />
    </com.google.android.material.textfield.TextInputLayout>


    <Button
        android:id="@+id/btn_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/txt_skip"
        android:background="@drawable/button"
        android:text="Update Profile" />

</LinearLayout>

用户.java

public class User {
    private String name,email,address,phoneNumber,gender;

    public User(){

    }

    public User(String name, String email, String address, String phoneNumber, String gender){
        this.name= name;
        this.email=email;
        this.address=address;
        this.phoneNumber=phoneNumber;
        this.gender=gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}

最佳答案

您只是在填充用户时在代码中输入了错误的顺序。 从您的 User 类中可以看出,您的构造函数将 gender 之前的 phoneNumber 作为参数。但在 new User(...) 初始化中,您将 edt_gender.getText().toString() 放在 phoneNumber 之前。

像这样更改您的代码:

void showUpdateDialog(String phoneNumber) {


        //init dialog
        bottomSheetDialog = new BottomSheetDialog(this);
        bottomSheetDialog.setTitle("one more step!");
        bottomSheetDialog.setCanceledOnTouchOutside(false);
        bottomSheetDialog.setCancelable(false);
        View sheetView = getLayoutInflater().inflate(R.layout.layout_update_information, null);

        Button btn_update = sheetView.findViewById(R.id.btn_update);
        TextInputEditText edt_name = sheetView.findViewById(R.id.edt_name);
        TextInputEditText edt_email = sheetView.findViewById(R.id.edt_email);
        TextInputEditText edt_address = sheetView.findViewById(R.id.edt_address);
        TextInputEditText edt_gender = sheetView.findViewById(R.id.edt_gender);

        btn_update.setOnClickListener(view -> {

            if (!dialog.isShowing())
                dialog.dismiss();

            User user = new User(edt_name.getText().toString(),
                    edt_email.getText().toString(),
                    edt_address.getText().toString(),
                    phoneNumber,
                    edt_gender.getText().toString(),
                    );
            userRef.document(phoneNumber)
                    .set(user)
                    .addOnSuccessListener(aVoid -> {
                        bottomSheetDialog.dismiss();
                        if (dialog.isShowing())
                            dialog.dismiss();
                        Toast.makeText(HomeActivity.this, " Thank You", Toast.LENGTH_SHORT).show();
                    }).addOnFailureListener(e -> {
                if (dialog.isShowing())
                    dialog.dismiss();
                bottomSheetDialog.dismiss();
                Toast.makeText(HomeActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
            });

        });

        bottomSheetDialog.setContentView(sheetView);
        bottomSheetDialog.show();

关于java - 输入已更新到数据库中的错误字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57840876/

相关文章:

android - opencv 校正轮廓

javascript - 在 Cloud Firestore 上创建文档时出错 - 权限被拒绝

json - 从 Firebase remoteMessage 中的嵌套 JSON 中提取数据

ios - 获取 Firebase 中每条消息的个人资料图片的最佳方式

java - 单个 Java 线程可以摧毁整个 JVM 吗?

java - 接口(interface)中需要私有(private)静态方法 - Java 9

java - 如何在 Spring Data Solr 1.0 中搜索短语

java - Play Framework 不是热重载

升级外接主板的安卓应用

android - fragment 中的上下文操作模式未显示