【问题标题】:Why i cant store info from my firestore to my java model class为什么我不能将信息从我的 firestore 存储到我的 java 模型类
【发布时间】:2021-04-23 12:22:59
【问题描述】:

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

虽然它似乎确实从数据库中检索数据,但它似乎并没有将该数据发送到模型类。我们认为此问题发生在 Store Activity 类中,并已在我们认为发生此问题的 cmets 中注明。

这是 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;
    }
}

【问题讨论】:

    标签: java google-cloud-firestore firebaseui


    【解决方案1】:

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

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

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

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

    另见:

    【讨论】:

      猜你喜欢
      • 2021-02-14
      • 2017-03-27
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 2022-01-27
      • 1970-01-01
      相关资源
      最近更新 更多