使用firebaseui

时间:2017-08-11 11:49:02

标签: android firebase firebase-realtime-database firebaseui

我希望我的卡片视图能够显示我的firebase数据库中的reviewTitle和reviewDetails。我该怎么做?我在想添加这段代码

'.child(userID).child("Reviews").child("reviewTitle")''.child(userID).child("Reviews").child("reviewDetails")'

但我不知道如何将它添加到我的代码中(我正在使用firebaseui)。我的卡片视图现在显示空白。或者,如果我应该使用另一组代码,请告诉我。

这就是我的firebase数据库目前的样子: here

这是我的代码:

我的片段

public class MyReviews extends Fragment {

    private RecyclerView rv;
    private FirebaseAuth firebaseAuth;
    private DatabaseReference mDatabase;

    LinearLayoutManager nwlinearLayoutManager;

    private FirebaseRecyclerAdapter<ReviewInformation, ReviewsHolder> mFirebaseAdapter;

    public MyReviews() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_my_reviews, container, false);

        rv = rootView.findViewById(R.id.rv_recycler_view);
        rv.setHasFixedSize(true);
        nwlinearLayoutManager = new LinearLayoutManager(getActivity());
        nwlinearLayoutManager.setStackFromEnd(true);


        firebaseAuth = FirebaseAuth.getInstance();
        FirebaseUser user = firebaseAuth.getCurrentUser();
        String userID = user.getUid();
        mDatabase = FirebaseDatabase.getInstance().getReference();
        Log.i("value1: ", mDatabase.toString());

        mFirebaseAdapter = new FirebaseRecyclerAdapter<ReviewInformation, ReviewsHolder>(
                ReviewInformation.class,
                R.layout.card_item,
                ReviewsHolder.class,
                mDatabase) {
            @Override
            public void populateViewHolder(ReviewsHolder holder, ReviewInformation chat, int position) {
                holder.reviewTitle.setText(chat.getReviewTitle());
                holder.reviewDetails.setText(chat.getReviewDetails());
            }
        };

        mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver(){
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount){
                super.onItemRangeInserted(positionStart, itemCount);
                int newsCount = mFirebaseAdapter.getItemCount();
                int lastVisiblePosition = nwlinearLayoutManager.findLastVisibleItemPosition();
                if(lastVisiblePosition == -1 || (positionStart>= (newsCount -1) && lastVisiblePosition == (positionStart -1))){
                    rv.scrollToPosition(positionStart);
                }
            }
        });
        rv.setLayoutManager(nwlinearLayoutManager);
        rv.setAdapter(mFirebaseAdapter);
        mFirebaseAdapter.notifyDataSetChanged();


        return rootView;
    }

    public static class ReviewsHolder extends RecyclerView.ViewHolder {

        public TextView reviewTitle;
        public TextView reviewDetails;
        ImageView img;
        public ReviewsHolder(View itemView) {
            super(itemView);

            this.reviewTitle= itemView.findViewById(R.id.tv_text);
            this.reviewDetails= itemView.findViewById(R.id.tv_blah);
            //img= itemView.findViewById(R.id.beachImage);


        }
    }
}

我的数据

public class ReviewInformation {

    public String reviewTitle;
    public String reviewDetails;

    public ReviewInformation() {

    }

    public ReviewInformation(String reviewTitle, String reviewDetails) {
        this.reviewTitle = reviewTitle;
        this.reviewDetails = reviewDetails;
    }

    public String getReviewTitle() {
        return reviewTitle;
    }

    public void setReviewTitle(String reviewTitle) {
        this.reviewTitle = reviewTitle;
    }

    public String getReviewDetails() {
        return reviewDetails;
    }

    public void setReviewDetails(String reviewDetails) {
        this.reviewDetails = reviewDetails;
    }
}

1 个答案:

答案 0 :(得分:0)

你必须引用孩子

mDatabase = FirebaseDatabase.getInstance().getReference().child("Reviews");

希望这会起作用