FirebaseUI Recyclerview如何显示用户数据

时间:2018-06-05 19:12:08

标签: android firebase firebase-realtime-database android-recyclerview firebaseui

我在Android和FirebaseUI中很新鲜,我在recyclerview中遇到了用户数据问题。我不知道如何在FirebaseUI recylerview中显示用户数据。我尝试了一些解决方案,但recyclerview仍然是空的。 当我输入用户ID时,在显示子数据之前引用。 我做错了什么?

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

    mTextDescription = v.findViewById(R.id.textDescription);
    mTextDate = v.findViewById(R.id.textDate);
    mTextValue = v.findViewById(R.id.textValue);
    mRecyclerView = v.findViewById(R.id.myRecyclerView);

    myRef = FirebaseDatabase.getInstance().getReference().child("Expense");

    mAuth = FirebaseAuth.getInstance();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();

            if (null != user) {

                onSignedInInitialize(user);
                Log.d(TAG, "onAuthStateChanged: signed_in: " + user.getUid());
            } else {

                Log.d(TAG, "onAuthStateChanged: signed_out: ");

            }


        }
    };

    return v;

}

private void onSignedInInitialize(FirebaseUser user) {
    user.reload();
    if (null != user) {
        FirebaseUser currentUser = mAuth.getCurrentUser();
    }
}


public static class ExpenseViewHolder extends RecyclerView.ViewHolder {
    View view;
    public ExpenseViewHolder(View itemView) {
        super(itemView);
        view=itemView;
    }
    public void setDescription(String description)
    {
        TextView exp_description= view.findViewById(R.id.textDescription);
        exp_description.setText(description);
    }
    public void setDate(String date)
    {
        TextView exp_date= view.findViewById(R.id.textDate);
        exp_date.setText(date);
    }
    public void setValue(String value)
    {
        TextView exp_value= view.findViewById(R.id.textValue);
        exp_value.setText(value);
    }
}

@Override
public void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
    firebaseRecyclerAdapter.startListening();


}

@Override
public void onStop() {
    super.onStop();
    firebaseRecyclerAdapter.stopListening();
}

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mRecyclerView = view.findViewById(R.id.myRecyclerView);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    query = FirebaseDatabase.getInstance().getReference().child("Expense");
    FirebaseRecyclerOptions<Expense>  options = new FirebaseRecyclerOptions.Builder<Expense>()
            .setQuery(query, Expense.class).build();
    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Expense, ExpenseViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull ExpenseViewHolder holder, int position, @NonNull Expense model) {

            holder.setDescription(model.getDescription());
            holder.setDate(model.getDate());
            holder.setValue(model.getValue());



        }

        @NonNull
        @Override
        public ExpenseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view;
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
            return new ExpenseViewHolder(view);
        }
    };
    mRecyclerView.setAdapter(firebaseRecyclerAdapter);
    Objects.requireNonNull(getActivity()).setTitle("Expense");


}

@Override
public void onResume() {
    super.onResume();
    if (null != mAuthListener) {
        mAuth.addAuthStateListener(mAuthListener);
    }
}

Recyclerwview对新秀来说有点难度

0 个答案:

没有答案