获取保存在 Firebase 中的选定单选按钮的值,并将其设置为用户配置文件中的默认状态

Get value of selected radio button which is saved in Firebase, and set it as default state in user profile

public void showFields(){
    databaseReference = FirebaseDatabase.getInstance().getReference().child("user");
    databaseReference.child(User_Login.userid).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot){
            id1.setText(User_Login.userid);
            name1.setText(dataSnapshot.child("name").getValue().toString());
            password1.setText(dataSnapshot.child("password").getValue().toString());
            mobile1.setText(dataSnapshot.child("mobile").getValue().toString());
            city1.setText(dataSnapshot.child("city").getValue().toString());
            area1.setText(dataSnapshot.child("area").getValue().toString());
             //gender.setSelected(dataSnapshot.child("area").getValue().toString());       
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

如何将性别单选按钮设置为其从数据库中选择的值,就像我为编辑文本所做的那样。

尝试以下代码:

if (list.contains(dataSnapshot.child("gender").getValue().toString()){
                            int pos = list.indexOf(dataSnapshot.child("gender").getValue().toString());

                            gender.setSelection(pos);
                        }

其中列表是设置为您的性别微调器适配器的数组列表。

试试这个

首先,您必须将性别值字符串存储到 Firebase。

gender.equalsIgnoreCase 将检查 gender 的值是否等于 Man (case man),如果为真,则单选按钮 male 将设置 Checked(true)。

public void showFields(){
databaseReference = FirebaseDatabase.getInstance().getReference().child("user");
databaseReference.child(User_Login.userid).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot){
        id1.setText(User_Login.userid);
        name1.setText(dataSnapshot.child("name").getValue().toString());
        password1.setText(dataSnapshot.child("password").getValue().toString());
        mobile1.setText(dataSnapshot.child("mobile").getValue().toString());
        city1.setText(dataSnapshot.child("city").getValue().toString());
        area1.setText(dataSnapshot.child("area").getValue().toString());
         String gender = dataSnapshot.child("gender").getValue().toString();
            if(gender.equalsIgnoreCase("Man")){
                mGenderMan.setChecked(true);
            }else if(gender.equalsIgnoreCase("Woman")){
                mGenderWoman.setChecked(true);
            }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});}