从Firebase DB写入片段

时间:2017-12-04 20:15:47

标签: android firebase textview android-fragmentactivity

public class ProfileFragment extends Fragment {

View myVeiw;
private TextView PUsername_tv, PForename_tv, PSurname_tv;
private FirebaseAuth auth;
private FirebaseAuth.AuthStateListener authListener;
private DatabaseReference mDatabase;

@Override
public void onStart(){
    super.onStart();
    auth.addAuthStateListener(authListener);//attach AuthStateListner(authListner)
}


public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    myVeiw = inflater.inflate(R.layout.profile_page, container, false);


    PUsername_tv = (TextView) getView().findViewById(R.id.tvPUsername);
    PForename_tv = (TextView) getView().findViewById(R.id.tvPForename);
    PSurname_tv = (TextView) getView().findViewById(R.id.tvPSurname);

    auth = FirebaseAuth.getInstance();
    authListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (firebaseAuth.getCurrentUser() != null) {
                mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
                mDatabase.child(firebaseAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                    PUsername_tv.setText(String.valueOf(dataSnapshot.child("username").getValue()));
                    PForename_tv.setText(String.valueOf(dataSnapshot.child("forename").getValue()));
                    PSurname_tv.setText(String.valueOf(dataSnapshot.child("surname").getValue()));
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

            }
        }
    };



    return myVeiw;
}

}

我试图获取用户登录的数据显示在名为Profile的片段内(我在android studio中使用了导航抽屉)我以前做过这个但是只有一个Activity而不是一个片段因此为什么我需要帮助:d

0 个答案:

没有答案