无法从Firebase检索当前用户的数据

时间:2019-02-19 09:14:11

标签: java android firebase firebase-authentication

我使用了很多技术,但是似乎没有任何效果!我想设计一个导航抽屉,其中必须获取当前用户详细信息并将其显示在MyAccount Fragment中。 这是我的代码:

public class MyAccountFragment extends Fragment {
    DatabaseReference ref;
    FirebaseAuth auth;
    FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
    Toolbar toolbar;

    @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);


        TextView acc_name,acc_mobile,acc_email,acc_dob,acc_gender,acc_city,acc_state,acc_sml,acc_syp,acc_aadhar_no;
            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);
            ref= FirebaseDatabase.getInstance().getReference();
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                   String uid=user.getUid();
                    Log.i(TAG,"Uid :"+uid);
                    Map<String, String> map = (Map<String, String>) dataSnapshot.child("data").getValue();
                    String name=map.get("name1");
                    String email=map.get("emailid1");
                    String phone=map.get("phone1");
                    String gender=map.get("gender1");
                    Log.v("E_VALUE","Name:"+name);
                    Log.v("E_VALUE","Email:"+email);
                    Log.v("E_VALUE","Phone:"+phone);
                    Log.v("E_VALUE","Gender:"+gender);



                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Log.i(TAG,"Erri:");
                }
            });



          return view;
        }

    }

上面的代码给出了NULL值。

这是我的Logcat:

02-19 13:48:00.253 21620-21620/com.sahayatra.samyatra V/E_VALUE: Name:null
    Email:null
02-19 13:48:00.254 21620-21620/com.sahayatra.samyatra V/E_VALUE: Phone:null
    Gender:null

在数据库中,我有一个名为“数据”的子项,所有详细信息都存储在该子项下。

我的数据库结构

enter image description here

这是将数据插入Firebase中的代码

 mDatabase = FirebaseDatabase.getInstance().getReference();


        buttonregister.setOnClickListener(new View.OnClickListener() {
                                           public void onClick(View view) {
                                               b=checkdata();
                                               if(b==1)
                                               {
                                               inputStream=getResources().openRawResource(R.raw.licence_dataset);
                                               BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
                                                   String csvLine;
                                               try
                                               {


                                                   while((csvLine=reader.readLine())!=null) {
                                                       ids = csvLine.split(",");


                                                           if ((lic_no.getText().toString()).equals(ids[0])&&(ids[1].equals("Four"))) {
                                                               User2 user2 = new User2(editTextPhone,name, emailid,dob,address,state,occupation,car_number,car_model,car_colour,lic_no,sharelocmobile,gender1,preference,downloadUri);
                                                               mDatabase.child("data").push().setValue(user2);
                                                               startActivity(new Intent(Main5Activity.this, Main8navigation.class));
                                                               flag = 1;
                                                               break;
                                                           }
                                                        else
                                                           {
                                                               flag=0;
                                                           }

                                                   }



                                               }catch (IOException e)
                                               {
                                                   throw new RuntimeException("error in running CSV file");
                                               }

                                                   if(flag==0)
                                                   {
                                                       lic_no.setError("License authentication failed");
                                                       lic_no.requestFocus();
                                                   }

                                               }
                                               else
                                               {
                                                   Toast.makeText(getApplicationContext(),
                                                           "Enter Valid Details", Toast.LENGTH_LONG).show();

                                               }
                                           }
                                       }
        );
    }

这是我们的用户类别:-

public class User2 {
    public String phone1;
    public String name1;
    public String emailid1;
    public String dob1;
    public String address1;
    public String state1;
    public String occupation1;
    public String car_number1;
    public String car_model1;
    public String car_colour1;
    public String lic_no1;
    public String sharelocmobile1;
    public String gender2;
    public String preference1;
    public String profile_picture_url1,fromo,too;


    // Default constructor required for calls to
    // DataSnapshot.getValue(User.class)
    public User2(){

    }


    public User2(EditText editTextPhone, EditText name, EditText emailid, EditText dob, EditText address, EditText state, EditText occupation, EditText car_number, EditText car_model, EditText car_colour, EditText lic_no, EditText sharelocmobile, String gender1, String preference, Uri downloadUri) {
        phone1 = editTextPhone.getText().toString();
        name1 = name.getText().toString();
        emailid1 = emailid.getText().toString();
        dob1 = dob.getText().toString();
        address1 = address.getText().toString();
        state1 = state.getText().toString();
        occupation1 = occupation.getText().toString();
        car_number1 = car_number.getText().toString();
        car_model1 = car_model.getText().toString();
        car_colour1 = car_colour.getText().toString();
        lic_no1 = lic_no.getText().toString();
        sharelocmobile1 = sharelocmobile.getText().toString();
        this.gender2 = gender1;
        this.preference1 = preference;
        profile_picture_url1 = downloadUri.toString();
    }

1 个答案:

答案 0 :(得分:0)

尝试一下, ref = FirebaseDatabase.getInstance()。getReference(userID); 您的数据库唯一的userID,它将基于userID返回数据库。