如何从Firebase数据库中获取价值?

时间:2016-12-19 07:16:48

标签: android firebase firebase-realtime-database

如何将Firebase用户数据从DatabaseHandler转移到MypageHolderListFragment?

public class MypageHolderListFragment extends BaseFragment {

private static final String TAG ="MypageHolderListFr.";

private ImageView mFaceImage;
private TextView mTextViewUsrName;
private TextView mTextViewUsrEmail;

private String mDisplayName;
private String mUserEmail;
private String UID;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
}

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

    mFaceImage = (ImageView) view.findViewById (R.id.imageView);
    mTextViewUsrEmail = (TextView) view.findViewById (R.id.user_email);
    mTextViewUsrName = (TextView) view.findViewById (R.id.user_name);

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    DatabaseHandler mDatabaseHandler = new DatabaseHandler();

    if (user != null) {
        // User is signed in

        UID = user.getUid ();
        mDisplayName = user.getDisplayName();
        mUserEmail = user.getEmail();
        mTextViewUsrEmail.setText (mUserEmail);

        Log.d (TAG, "UID: " + UID);
        Log.d (TAG, "DisplayName: " + mDisplayName);
        Log.d (TAG, "UserEmail: " + mUserEmail);


        User login_user = mDatabaseHandler.getCurrentUser (UID);

        Log.d (TAG, "Login_user username: " + login_user.username);
        Log.d (TAG, "Login_user useremail: " + login_user.email);


    } else {
        Log.d (TAG, "null.");
    }
    return view;
}
}

=============================================== ==

public class DatabaseHandler {

private static final String TAG = "DatabaseHandler";

public DatabaseHandler() {

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

    }

private DatabaseReference mDatabase;

public void saveUser(String userId, String email, String username ) {

        User user = new User(username, email);
        mDatabase.child("users").child(userId).setValue(user);

}

public User getCurrentUser(String  userId) {

    mDatabase.child("users").child(userId).addListenerForSingleValueEvent(
            new ValueEventListener () {

                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    User user = dataSnapshot.getValue(User.class);

                    if (user == null) {

                        Log.d (TAG, "user is null");

                    } else {
                        Log.d (TAG, "user username: " + user.username);
                        Log.d (TAG, "user email :" + user.email);

                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.w(TAG, "getUser:onCancelled", databaseError.toException());
                }
            });

    return user; //**error event because of void method onDataChange()**
}
}

请帮帮我。

0 个答案:

没有答案