为什么我不能将firestore中的信息存储到java模型类中

ldioqlga  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(354)

storeactivity类应该从数据库中检索数据并将其发送给store模型类,同时设置一个recycler视图来显示信息。还有一个storeadapter类,它处理recycler视图的各个项目,它从storemodel类中检索所需的信息。
虽然它似乎从数据库中检索数据,但似乎并没有将数据发送到模型类。我们认为这个问题发生在store activity类中,并在评论中指出了我们认为问题发生的地方。
这是firestore数据库的结构,store中的所有字段都是字符串:

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

涉及的代码

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;
    }
}
lvjbypge

lvjbypge1#

firebase/firestore使用javabean命名约定来确定java字段和文档中字段之间的Map。
这意味着你的 public String getStoreID() 方法被解释为 storeID 文档中的字段。但你的文件里有什么 StoreID ,带大写字母 S . 自 storeID 以及 StoreID 不完全相同,firebase在读/写数据时不Map它们。
您可以选择 storeID 作为文档中的字段名,或使用 PropertyName 每个getter和设置上的注解:

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

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

另请参见:
在firestore文档的java类中指定序列化名称

相关问题