为什么我无法将信息从我的 firestore 存储到我的 java 模型 class

Why i cant store info from my firestore to my java model class

Store Activity Class 应该从数据库中检索数据并将其发送给 Store Model Class 以及设置回收器视图以显示信息。还有一个 Store Adapter class 处理 Recycler View 的各个项目,它从 Store Model Class.

检索它需要的信息

虽然它似乎确实从数据库中检索数据,但它似乎并未将该数据发送到模型 class。我们认为此问题发生在商店 Activity Class 中,并在评论中指出了我们认为该问题发生的地方。

这是Firestore Database的结构,Store中的所有字段都是String:

这是应用程序当前显示的内容

涉及的代码

package com.example.deliveryapp;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;


public class StoreActivity extends AppCompatActivity{
    //Gets the current instance of the database
    private FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
    //Stores the Collection Path for Store
    private CollectionReference StoreRef = firebaseFirestore.collection("Store");
    //Variable for storing the RecyclerView
    private RecyclerView sFirestoreList;
    StoreAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stores);

        //Gets Current Instance of Database
        firebaseFirestore = FirebaseFirestore.getInstance();
        StoreRef = firebaseFirestore.collection("Store");
        sFirestoreList = findViewById(R.id.StoreList_rv);

        sFirestoreList.setLayoutManager(new LinearLayoutManager(this));

        //Creates the Query for getting information from the Database
        Query query = StoreRef;

        //Sends the Query to the Database and send that information to the Model Class
        //reminder to self the problem is in this part (Possibly), the app is retrieving data from the Database however can't seem to send it to the model class
        FirestoreRecyclerOptions<StoreModel> options = new FirestoreRecyclerOptions.Builder<StoreModel>()
                .setQuery(query, StoreModel.class)
                .build();

        //Connects the activity to the Adapter Class
        adapter = new StoreAdapter(options);
        //Connects the RecyclerView to the Adapter Class
        sFirestoreList.setAdapter(adapter);
    }

    // Gets the App to start reading data from the Database
    @Override
    protected void onStart(){
        super.onStart();
        adapter.startListening();
    }

    // Gets the App to Stop reading data from the Database
    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}



package com.example.deliveryapp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;

public class StoreAdapter extends FirestoreRecyclerAdapter<StoreModel, StoreAdapter.StoreViewholder> {

    public StoreAdapter(@NonNull FirestoreRecyclerOptions<StoreModel> options){
        super(options);
    }

    //Binds the data from the model class to Adapter's variables
    @Override
    protected void onBindViewHolder(@NonNull StoreViewholder holder, int position, @NonNull StoreModel model){
        holder.storeName.setText(model.getStoreName());
        holder.storeAddress.setText(model.getStoreAddress());
        holder.storeID.setText(model.getStoreID());
        //holder.storeID.setText("Test ID");
        //holder.storeName.setText("Test Store");
        //holder.storeAddress.setText("Test Address");
    }

    //Finds the display card for the data to be printed on
    @NonNull
    @Override
    public StoreViewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.storelist, parent, false);
        return new StoreAdapter.StoreViewholder(view);
    }

    //Prints the Store Information to the Activity Display
    class StoreViewholder extends RecyclerView.ViewHolder {
        TextView storeID, storeName, storeAddress;
        //Finds the Textviews by their ID and sets the their text
        public StoreViewholder(@NonNull View itemView){
            super(itemView);
            storeID = itemView.findViewById(R.id.Store_ID);
            storeName = itemView.findViewById(R.id.Store_Name);
            storeAddress = itemView.findViewById(R.id.Store_Address);
        }
    }
}


package com.example.deliveryapp;

public class StoreModel {
    private String storeID;
    private String storeName;
    private String storeAddress;

    //private String storeID = "TestID";
    //private String storeName = "TestName";
    //private String storeAddress = "TestAddress";

    //Empty Constructore for Firebase
    public StoreModel(){
        //Empty
    }

    //Constructor for gathering information from the firestore
    public StoreModel(String StoreAddress, String StoreID, String StoreName) {
        this.storeID = StoreID;
        this.storeName = StoreName;
        this.storeAddress = StoreAddress;
    }

    // Gets the Store ID
    public String getStoreID() {
        return storeID;
    }

    // Sets the Store ID
    public void setStoreID(String StoreID) {
        this.storeID = StoreID;
    }

    // Gets the Store Name
    public String getStoreName() {
        return storeName;
    }

    // Sets the Store Name
    public void setStoreName(String StoreName) {
        this.storeName = StoreName;
    }

    // Gets the Store Address
    public String getStoreAddress() {
        return storeAddress;
    }

    // Sets the Store Address
    public void setStoreAddress(String StoreAddress) {
        this.storeAddress = StoreAddress;
    }
}

Firebase/Firestore 使用 JavaBean 命名约定来确定 Java 字段与文档中字段之间的映射。

这意味着您的 public String getStoreID() 方法被解释为文档中的 storeID 字段。但是您在文档中实际拥有的是 StoreID,大写 S。由于 storeIDStoreID 不完全相同,Firebase 在 reading/writing 数据时不会映射它们。

您可以在文档中使用 storeID 作为字段名称,或者在每个 getter 和设置上使用 PropertyName annotation

@PropertyName("StoreID")
public String getStoreID() {
    return storeID;
}

@PropertyName("StoreID")
public void setStoreID(String StoreID) {
    this.storeID = StoreID;
}

另见: