Firestore数据未显示在RecyclerView中

时间:2019-04-08 01:29:28

标签: android firebase android-recyclerview google-cloud-firestore recycler-adapter

我正在尝试将我的Firestore数据放入Android的recyclerview中。该应用程序没有错误,但是没有数据显示。

public class Diseaselist extends AppCompatActivity {
    private TextView textView;
    private FirebaseFirestore mDatabaseRef;

    private Query mChartsQuery;
    private RecyclerView mRecycler;

    private FirebaseAuth mAuth;
    private FirestoreRecyclerAdapter<Upload, ProductViewHolder> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diseaselist);


        RecyclerView recyclerView = findViewById(R.id.goodmeme);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
        mAuth = FirebaseAuth.getInstance();
        FirebaseUser currentUser = mAuth.getCurrentUser();
        String useruid = currentUser.getUid();


        Query query = rootRef.collection("users").document(useruid).collection("diagnoses")
                .orderBy("disease", Query.Direction.ASCENDING);

        FirestoreRecyclerOptions<Upload> options = new FirestoreRecyclerOptions.Builder<Upload>()
                    .setQuery(query, Upload.class)
                .build();
        adapter = new FirestoreRecyclerAdapter<Upload, ProductViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull  ProductViewHolder holder, int position, @NonNull Upload productModel) {
                holder.setProductName(productModel.getDisease());
            }

            @NonNull
            @Override
            public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
               android.view.View views = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_diseaselist, parent, false);
                return new ProductViewHolder(views);

            }
        };
        recyclerView.setAdapter(adapter);


    }
    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (adapter != null) {
            adapter.stopListening();
        }
    }
    private class ProductViewHolder extends RecyclerView.ViewHolder {
        private android.view.View view;

        ProductViewHolder(android.view.View itemView) {
            super(itemView);
            view = itemView;
        }

        void setProductName(final String productName) {
            CardView cview =view.findViewById(R.id.cardview);
            textView = view.findViewById(R.id.texty);
            textView.setText(productName);
            cview.setOnClickListener(new android.view.View.OnClickListener() {
                @Override
                public void onClick(android.view.View view) {
                    Toast.makeText(getApplicationContext(), productName, Toast.LENGTH_SHORT).show();
                }
            });
        }

    }

布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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="match_parent"
    android:padding="16dp"

    tools:context=".Diseaselist"
    tools:orientation="vertical">
    <TextView
        android:id="@+id/texty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="30sp"/>

    <android.support.v7.widget.CardView
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">

            <ImageView
                android:id="@+id/person_photo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="16dp" />

            <TextView
                android:id="@+id/person_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/person_photo"
                android:textSize="30sp" />

            <TextView
                android:id="@+id/person_age"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/person_name"
                android:layout_toRightOf="@+id/person_photo" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/goodmeme"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Firestore structure Firestore Structure Screenshot 2

上传课程代码:

package com.Provendor.Provendor;

import android.os.Parcel;
import android.os.Parcelable;

import java.io.Serializable;
import java.util.Calendar;

public class Upload implements Parcelable {
    private String mName;
    private String mImageUrl;
    private String mdisease;
    private String mdate;
    private float mconfidence;
    @Override
    public int describeContents() {
        return 0;
    }

    // write your object's data to the passed-in Parcel
    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeFloat(mconfidence);
        out.writeString(mName);
        out.writeString(mImageUrl);
        out.writeString(mdisease);
        out.writeString(mdate);
    }

    // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<Upload> CREATOR = new Parcelable.Creator<Upload>() {
        public Upload createFromParcel(Parcel in) {
            return new Upload(in);
        }

        public Upload[] newArray(int size) {
            return new Upload[size];
        }
    };

    // example constructor that takes a Parcel and gives you an object populated with it's values
    private Upload(Parcel in) {
        mconfidence = in.readInt();
        mName  = in.readString();
        mImageUrl  = in.readString();
        mdisease = in.readString();
        mdate = in.readString();

    }

    public Upload() {
        mName= ""; //empty constructor needed
    }

    public Upload(String name, String imageUrl, String disease, float confidence) {
        if (name.trim().equals("")) {
            name = "No Name";
        }
        mdisease=disease;
        mdate=java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
        mName = name;
        mImageUrl = imageUrl;
        mconfidence=confidence;
    }

    public String getName() {
        return mName;
    }
    public String getDisease() {
        return mdisease;
    }
    public float getConfidence() {
        return mconfidence;
    }
    public String getDate() {
        return mdate;
    }
    public void setName(String name) {
        mName = name;
    }
    public void setdate() {
        mdate=java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
    }
    public void setDisease(String disease) {
        mdisease = disease;
    }
    public void setConfidence(float confidence) {
        mconfidence = confidence;
    }

    public String getImageUrl() {
        return mImageUrl;
    }

    public void setImageUrl(String imageUrl) {
        mImageUrl = imageUrl;
    }
}

我希望输出结果基于Firestore集合生成疾病列表,但是recyclerview保留为空。连接调试时,不会出现任何错误!感谢您查看!

1 个答案:

答案 0 :(得分:0)

您没有从数据库中得到任何东西,因为Upload类中的字段名称与数据库中存在的字段名称不同。两者应该匹配。为了解决这个问题,您可以更改Upload类中所有字段的名称,以匹配数据库中存在的属性,或者可以使用批注。因为我看到您正在使用私有字段和公共获取程序,所以应该仅在获取程序的前面使用PropertyName批注,例如getName()获取程序的外观应如下所示:

@PropertyName("name")
public String getName() {
    return mName;
}