如何将一个字符串数组列表存储到Firebase中?

huangapple go评论77阅读模式
英文:

How can I store an String Arraylist into Firebase?

问题

我是Android的新手,我有一个用户爱好的ArrayList。我想将这些值存储到我的User类中,但我不知道如何存储。

我的Intent值:

comp_phoneNumber = getIntent().getStringExtra("userPhone");
user_FirstName = getIntent().getStringExtra("user_FirstName");
user_Gender = getIntent().getStringExtra("user_Gender");
user_Birthdate = getIntent().getStringExtra("user_Birthdate");

我的Getter-Setter类:

public class UserHelperClass {

    String comp_phoneNumber, user_FirstName, user_Gender, user_Birthdate;

    UserHelperClass() {
    }

    public UserHelperClass(String comp_phoneNumber, String user_FirstName, String user_Gender, String user_Birthdate) {
        this.comp_phoneNumber = comp_phoneNumber;
        this.user_FirstName = user_FirstName;
        this.user_Gender = user_Gender;
        this.user_Birthdate = user_Birthdate;
    }

    public String getComp_phoneNumber() {
        return comp_phoneNumber;
    }

    public void setComp_phoneNumber(String comp_phoneNumber) {
        this.comp_phoneNumber = comp_phoneNumber;
    }

    public String getUser_FirstName() {
        return user_FirstName;
    }

    public void setUser_FirstName(String user_FirstName) {
        this.user_FirstName = user_FirstName;
    }

    public String getUser_Gender() {
        return user_Gender;
    }

    public void setUser_Gender(String user_Gender) {
        this.user_Gender = user_Gender;
    }

    public String getUser_Birthdate() {
        return user_Birthdate;
    }

    public void setUser_Birthdate(String user_Birthdate) {
        this.user_Birthdate = user_Birthdate;
    }

}

在我的Last_Activity中,我使用以下代码存储这些Intent值。

public void storeNewUserData() {
    rootNode = FirebaseDatabase.getInstance();
    reference = rootNode.getReference("Users");

    UserHelperClass addNewUser = new UserHelperClass(comp_phoneNumber, user_FirstName, user_Gender, user_Birthdate);
    reference.child(comp_phoneNumber).setValue(addNewUser);
}

我想将我的Hobby String ArrayList值与这些值一起存储。如何存储呢?

英文:

I am new in Android, I have an ArrayList that hobbies of users. I want to store these values into my User class but I don't know how to store.

My intent values:

    comp_phoneNumber = getIntent().getStringExtra("userPhone");
    user_FirstName = getIntent().getStringExtra("user_FirstName");
    user_Gender = getIntent().getStringExtra("user_Gender");
    user_Birthdate = getIntent().getStringExtra("user_Birthdate");

My Getter-Setter class:

public class UserHelperClass {

String comp_phoneNumber, user_FirstName, user_Gender, user_Birthdate;

UserHelperClass() {
}

public UserHelperClass(String comp_phoneNumber, String user_FirstName, String user_Gender, String user_Birthdate) {
    this.comp_phoneNumber = comp_phoneNumber;
    this.user_FirstName = user_FirstName;
    this.user_Gender = user_Gender;
    this.user_Birthdate = user_Birthdate;
}

public String getComp_phoneNumber() {
    return comp_phoneNumber;
}

public void setComp_phoneNumber(String comp_phoneNumber) {
    this.comp_phoneNumber = comp_phoneNumber;
}

public String getUser_FirstName() {
    return user_FirstName;
}

public void setUser_FirstName(String user_FirstName) {
    this.user_FirstName = user_FirstName;
}

public String getUser_Gender() {
    return user_Gender;
}

public void setUser_Gender(String user_Gender) {
    this.user_Gender = user_Gender;
}

public String getUser_Birthdate() {
    return user_Birthdate;
}

public void setUser_Birthdate(String user_Birthdate) {
    this.user_Birthdate = user_Birthdate;
}

}

In my Last_Activity, I store these intent values with this code.

public void storeNewUserData() {
    rootNode = FirebaseDatabase.getInstance();
    reference = rootNode.getReference("Users");

    UserHelperClass addNewUser = new UserHelperClass(comp_phoneNumber, user_FirstName, user_Gender, user_Birthdate);
    reference.child(comp_phoneNumber).setValue(addNewUser);
}

I want to store my Hobby String Arraylist values with these values. How can I store?

答案1

得分: 0

可以直接使用 HashMap 来存储值一旦获取到值无需先存储在类中或跳转到另一个活动中除非有必要这样做

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");

HashMap<String, Object> userMap = new HashMap<>();
userMap.put("comp_phoneNumber", addNewUser.getComp_phoneNumber());
userMap.put("第二个字段", value);
userMap.put("第三个字段", value);
userMap.put("第四个字段", value);
ref.child(comp_phoneNumber).updateChildren(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {

        if(task.isSuccessful()){

            Toast.makeText(getActivity(), "用户已成功添加", Toast.LENGTH_SHORT).show();
            
        }

        else {
            Toast.makeText(getContext(), "网络错误", Toast.LENGTH_SHORT).show();
        }
    }
});

像这样,您应该能够添加这些详细信息。虽然我对您关于爱好的发言感到困惑?


<details>
<summary>英文:</summary>

You can simply use a HashMap to store the values as soon as you get them without needing to store them in a class first or going to another activity. Unless that is necessary

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");

HashMap<String, Object> userMap = new HashMap<>();
userMap. put("comp_phoneNumber", addNewUser.getComp_phoneNumber());
userMap. put("2nd field", value);
userMap. put("3rd field", value);
userMap. put("4th field", value);
ref.child(comp_phoneNumber).updateChildren(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {

    if(task.isSuccessful()){

        Toast.makeText(getActivity(), &quot;User added successfully&quot;, Toast.LENGTH_SHORT).show();
        
    }

    else {
        Toast.makeText(getContext(), &quot;Network Error&quot;, Toast.LENGTH_SHORT).show();
    }
}

});


Like this you should be able to add the details. Although I am confused as to what was that you wanted to say about Hobbies?

</details>



huangapple
  • 本文由 发表于 2020年8月29日 21:18:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63647392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定