Firebase RecyclerView不会从数据库中检索项目并在屏幕上显示它们。屏幕为空

时间:2020-02-04 12:20:27

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

我正在尝试将数据库中的某些商品显示到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>

图片数据库: enter image description here

1 个答案:

答案 0 :(得分:0)

您已经为它创建了查询,但是您没有使用它。

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。