简体   繁体   中英

Firebase how to properly retrieve data and display data from the database

I am attempting to pull data from firebase and display the user's name and status. I am currently encountering a null point exception.

public class ProfileFragment extends Fragment {

private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.fragment_profile, container, false);

    CircleImageView displayProfilePicture = view.findViewById(R.id.settings_profile_picture);
    final TextView mName = view.findViewById(R.id.settings_user_name);
    final TextView mStatus = view.findViewById(R.id.settings_user_status);

    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();

    String current_uid = Objects.requireNonNull(mCurrentUser).getUid();

    mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);

    mUserDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            String name = (Objects.requireNonNull(dataSnapshot.child("name").getValue())).toString();
            String status = Objects.requireNonNull(dataSnapshot.child("status").getValue()).toString();
            String image = Objects.requireNonNull(dataSnapshot.child("image").getValue()).toString();
            String thumb_image = Objects.requireNonNull(dataSnapshot.child("thumb image ").getValue()).toString();

            mName.setText(name);
            mStatus.setText(status);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }


    });
    return view;
}}

I have also tried to implement this in a different way using this code but i am still failing to display the data.

public class ProfileFragment extends Fragment {

private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.fragment_profile, container, false);

    CircleImageView displayProfilePicture = view.findViewById(R.id.settings_profile_picture);
    final TextView mName = view.findViewById(R.id.settings_user_name);
    final TextView mStatus = view.findViewById(R.id.settings_user_status);

    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();

    String current_uid = Objects.requireNonNull(mCurrentUser).getUid();

    mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);

    mUserDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot datas: dataSnapshot.getChildren()){
                String keys=datas.getKey();
                String displayname=datas.child("name").getValue().toString();

                mName.setText(displayname);
        }}

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }


    });
    return view;
}}

and my database is structured like this:

数据库结构

Since you are already have the userId then you do not need to loop to be able to access the direct children.

Try the following:

 mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);

mUserDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String keys=dataSnapshot.getKey();
            String displayname=dataSnapshot.child("name").getValue().toString();

            mName.setText(displayname);
    }}

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }  
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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