简体   繁体   English

获取保存在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) {

        }
    });
}

How can I set the gender radio button to its selected value from the database, as I did for the Edit Text ones. 我如何将性别单选按钮设置为从数据库中选择的值,就像我对“编辑文本”所做的那样。

Try This 尝试这个

First, u have to store gender value string to Firebase. 首先,您必须将性别值字符串存储到Firebase。

gender.equalsIgnoreCase will check if the value of gender equals Man (case man), if true then the radiobutton male will setChecked(true). sex.equalsIgnoreCase将检查gender的值是否等于Man(case man),如果为true,则单选按钮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) {

    }
});}

Try following code : 尝试以下代码:

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

                            gender.setSelection(pos);
                        }

Where list is arraylist set to your gender spinner adapter. 其中list是arraylist设置为您的性别微调框适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM