0
我希望我的卡片視圖顯示我的firebase數據庫中的reviewTitle和reviewDetails。我應該怎麼做?我在想將此代碼添加使用firebaseui將firebase數據庫中的子數據添加到cardview中
'.child(userID).child("Reviews").child("reviewTitle")'
和'.child(userID).child("Reviews").child("reviewDetails")'
,但我不知道如何將它添加到我的代碼(我用firebaseui)。我的卡片視圖目前顯示空白。或者如果有另一套我應該使用的代碼,請讓我知道。
這是我的火力數據庫目前的樣子: 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;
}
}
這是行不通的。實際上,它導致我的整個卡片視圖根本不顯示 – ben
hey @Nithin Paul,我已經完成了mDatabase = FirebaseDatabase.getInstance()。getReference()。child(「Business」)。child(userID);相反,它的工作。除了現在有一個空的cardview隨之而來,我不想要。任何想法如何擺脫它? – ben