[英]Retrieve images from Firebase Database and display them on screen in my app
[英]Firebase RecyclerView Will Not Retreive Items from Database and Display them on Screen. Screen is Empty
我正在嘗試將我的數據庫中的一些項目顯示到我的 Recyclerview 中。 但是,我的屏幕上不會顯示任何內容。 我已經確認這些項目在數據庫中。 我查看了 StackOverflow 上的其他問題,我添加了 On Start Listening 和 OnStop Listening,但似乎沒有任何效果。 我真的很感激任何能讓我走向正確方向的幫助。 先感謝您。
模型類
<public class Posting_Class {
private String firstname;
private String lastname;
private String profileimage;
private String user_post;
public Posting_Class() {}
public Posting_Class(String firstname, String lastname, String profileimage, String user_post) {
this.firstname = firstname;
this.lastname = lastname;
this.profileimage = profileimage;
this.user_post = user_post;
}
public String getUser_post() {
return user_post;
}
public void setUser_post(String user_post) {
this.user_post = user_post;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getProfileimage() {
return profileimage;
}
public void setProfileimage(String profileimage) {
this.profileimage = profileimage;
}
}
帶有 RecylerView 布局的 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="@+id/main_recycle_view_post"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:padding="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/recycle_view_user_image"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="268dp"
android:src="@drawable/profile_place_holder"
app:layout_constraintEnd_toStartOf="@+id/options_menu"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/recycle_view_first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp"
android:text="First Name"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/recycle_view_user_image"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/recycle_view_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="90dp"
android:layout_marginTop="30dp"
android:text="Last Name"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/recycle_view_user_image"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/post_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="5dp"
android:text="Blog Post Date and Time"
app:layout_constraintStart_toEndOf="@+id/recycle_view_user_image"
app:layout_constraintTop_toBottomOf="@+id/recycle_view_first_name" />
<ImageView
android:id="@+id/recycle_view_image"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/post_words_placeholder"
app:srcCompat="@drawable/ic_rectangle" />
<TextView
android:id="@+id/post_words_placeholder"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="45dp"
android:layout_marginEnd="16dp"
android:text="Lorem ipsum dolor sit amet, soluta regione urbanitas vis in, qui elit populo ut. "
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/post_date" />
<TextView
android:id="@+id/comment_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="0"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/comment_icon"
app:layout_constraintTop_toBottomOf="@+id/recycle_view_image"
app:layout_constraintVertical_bias="0.876" />
<ImageView
android:id="@+id/comment_icon"
android:layout_width="28dp"
android:layout_height="24dp"
app:srcCompat="@drawable/ic_speech_bubble"
app:layout_constraintEnd_toStartOf="@+id/comment_count"
app:layout_constraintTop_toBottomOf="@+id/recycle_view_image"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="30dp"
android:layout_marginBottom="8dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="403dp" />
<TextView
android:id="@+id/peace_sign_count"
android:layout_width="24dp"
android:layout_height="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="1dp"
android:text="0"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/recycle_view_image" />
<ImageView
android:id="@+id/peace_sign_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/peace_sign_count"
app:layout_constraintTop_toBottomOf="@+id/recycle_view_image"
app:srcCompat="@drawable/ic_win" />
<ImageView
android:id="@+id/options_menu"
android:layout_width="22dp"
android:layout_height="25dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="23dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/recycle_view_user_image"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>>
查看持有人:
public class ViewHolder extends RecyclerView.ViewHolder {
View mView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mClickListener.onItemClick(view, getAdapterPosition());
}
});
}
public void setDetails(Context ctx, String firstname, String lastname, String profileimage, String user_post) {
//Views//
TextView mPost_words = mView.findViewById(R.id.post_words_placeholder);
TextView mpostfirstname = mView.findViewById(R.id.recycle_view_first_name);
TextView mpostlastname = mView.findViewById(R.id.recycle_view_last_name);
CircleImageView mpost_user_profile_picture = mView.findViewById(R.id.recycle_view_user_image);
ImageView recycle_place_holder = mView.findViewById(R.id.recycle_view_image);
TextView comment_count = mView.findViewById(R.id.comment_count);
ImageView comment_icon = mView.findViewById(R.id.comment_icon);
ImageView peace_icon = mView.findViewById(R.id.peace_sign_icon);
TextView peace_count = mView.findViewById(R.id.peace_sign_count);
ImageView recycle_view_menu_icon = mView.findViewById(R.id.options_menu);
TextView recycle_view_date_time = mView.findViewById(R.id.post_date);
//Views//
//set data to views//
mPost_words.setText(user_post);
mpostfirstname.setText(firstname);
mpostlastname.setText(lastname);
Picasso.get().load(profileimage).into(mpost_user_profile_picture);
//set data to views//
}
private ViewHolder.ClickListener mClickListener;
public interface ClickListener {
void onItemClick(View view,int position);
}
public void setOnClickListener(ViewHolder.ClickListener clickListener) {
mClickListener = clickListener;
}
}
適配器:
<private void show_data_from_firebase() {
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("Users");
options = new FirebaseRecyclerOptions.Builder<Posting_Class>().setQuery(UsersReference, Posting_Class.class).build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posting_Class, ViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Posting_Class posting_class) {
holder.setDetails(getApplicationContext(),posting_class.getFirstname(),posting_class.getLastname(),posting_class.getProfileimage(),posting_class.getUser_post());
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleview_layout_activity_wall, parent, false);
ViewHolder viewHolder = new ViewHolder(itemView);
viewHolder.setOnClickListener(new ViewHolder.ClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(Personal_Wall.this, "Hello", Toast.LENGTH_SHORT);
}
});
return viewHolder;
}
};
firebaseRecyclerAdapter.startListening();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
if (firebaseRecyclerAdapter !=null) {
firebaseRecyclerAdapter.stopListening();
}
}
包含 RecycleView 的一些活動 XML:
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_recycle_view_for_posts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="40dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_marginLeft="100dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "This is text to make the screen scroll"
android:textColor="#000"
android:layout_marginTop="400dp"
/>
</LinearLayout>
您已經為它創建了查詢,但您沒有使用它。
Query query = FirebaseDatabase.getInstance().getReference().child("Users");
//Here is your mistake, changed to query (before this UserReference)
options = new FirebaseRecyclerOptions.Builder<Posting_Class>().setQuery(query, Posting_Class.class).build();
更新:
將此屬性添加到您的類中。
private String firstname;
private String lastname;
private String profileimage;
private String user_post;
private String bannerImage; //Add here
之后,如果要存儲數據,可以設置為null。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.