无法更新Firebase中的数据

时间:2019-02-22 03:01:12

标签: java android

我想通过android studio更新Firebase数据库中的某些特定值。我已经使用地图检索了数据。

这是我的MyAccountFragment.java代码:-

public class MyAccountFragment extends Fragment {
    DatabaseReference ref;
    FirebaseAuth auth;
    FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
    Toolbar toolbar;
    Button update;
    int b;
     EditText acc_name,acc_mobile,acc_email,acc_dob,acc_gender,acc_city,acc_state,acc_sml,acc_syp,acc_aadhar_no;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_myaccount,container,false);
        toolbar = view.findViewById(R.id.toolbar);



            super.onCreate(savedInstanceState);
            acc_name=view.findViewById(R.id.acc_name);
            acc_mobile=view.findViewById(R.id.acc_mobile);
            acc_email=view.findViewById(R.id.acc_email);
            acc_dob=view.findViewById(R.id.acc_dob);
            acc_gender=view.findViewById(R.id.acc_gender);
            acc_city=view.findViewById(R.id.acc_city);
            acc_state=view.findViewById(R.id.acc_state);
            acc_sml=view.findViewById(R.id.acc_sml);
            acc_syp=view.findViewById(R.id.acc_syp);
            acc_aadhar_no=view.findViewById(R.id.acc_aadhar_no);
            update=view.findViewById(R.id.update);


            ref= FirebaseDatabase.getInstance().getReference(uid);
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();
                    String name=map.get("name1");
                    String email=map.get("emailid1");
                    String phone=map.get("phone1");
                    String dob=map.get("dob1");
                    String gender=map.get("gender2");
                    String city=map.get("address1");
                    String state=map.get("state1");
                    String sml=map.get("sharelocmobile1");
                    String preference=map.get("preference1");
                    String aadhar=map.get("aadhar_number1");

                    acc_name.setText(Html.fromHtml("<font><b>" + "NAME : " + "</b></font>" +name));

                    acc_email.setText(Html.fromHtml("<font><b>" + "EMAIL : "+ "</b></font>"+email));

                    acc_mobile.setText(Html.fromHtml("<font><b>" + "MOBILE No. : "+ "</b></font>"+phone));

                    acc_dob.setText(Html.fromHtml("<font><b>" + "DOB : "+ "</b></font>"+dob));

                    acc_gender.setText(Html.fromHtml("<font><b>" + "GENDER : "+ "</b></font>"+gender));

                    acc_city.setText(Html.fromHtml("<font><b>" + "CITY : "+ "</b></font>"+city));

                    acc_state.setText(Html.fromHtml("<font><b>" + "STATE : "+ "</b></font>"+state));

                    acc_sml.setText(Html.fromHtml("<font><b>" + "SHARING MOBILE No. : "+ "</b></font>"+sml));

                    acc_syp.setText(Html.fromHtml("<font><b>" + "YOUR PREFERENCE : "+ "</b></font>"+preference));

                    acc_aadhar_no.setText(Html.fromHtml("<font><b>" + "AADHAR No. : "+ "</b></font>"+aadhar));
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Log.i(TAG,"Error occured in connecting with database");
                }
            });

               update.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       b=checkDataEntered();
                       if(b==1)
                       {
                           mDatabase.child(uid).setValue(acc_city.getText().toString());
                           mDatabase.child(uid).setValue(acc_state.getText().toString());
                           mDatabase.child(uid).setValue(acc_sml.getText().toString());
                       }
                       else{
                           Toast.makeText(getActivity(),
                                   "Enter Valid Details", Toast.LENGTH_LONG).show();
                       }
                   }
               });

          return view;
        }
    boolean isEmpty(EditText text)
    {
        CharSequence str=text.getText().toString();
        return TextUtils.isEmpty(str);

    }
    int checkDataEntered() {
        if (isEmpty(acc_city))
        {
            acc_city.setError("This field is required");
            return 0;
        }
        if(isEmpty(acc_state))
        {
            acc_state.setError("This field is required");
            return 0;
        }
        if(isEmpty(acc_sml))
        {
            acc_sml.setError("This field is required");
            return 0;
        }
        return 1;
    }

    }

上面的代码给出了错误:-java.lang.String无法转换为java.util.Map

还有其他方法可以更新数据吗?

任何帮助将不胜感激。 谢谢

0 个答案:

没有答案