Store Activity Class is supposed to retrieve the data from the database and send it the Store Model Class as well as set up a recycler view to display information. There is also a Store Adapter class which handles to the individual items of the Recycler View, it retrieves the information it needs from the Store Model Class.
Whilst it does seem to retrieve data from the database, it does not seem to send that data to the model class. We believe this issue is occurring in the Store Activity Class and have noted in the comments where we think the issue is happening.
This is the structure of the Firestore Database, all of the fields in Store are String:
This what the app displays currently
the code involved
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 uses JavaBean naming convention to determine the mapping between the Java fields and the fields in the document.
This means that your public String getStoreID()
method gets interpreted as a storeID
field in the document. But wht you actually have in your document is StoreID
, with an uppercase S
. Since storeID
and StoreID
are not exactly the same, Firebase doesn't map them when reading/writing data.
You can either user storeID
as the field name in your document, or use a PropertyName
annotation on each of the getters and settings:
@PropertyName("StoreID")
public String getStoreID() {
return storeID;
}
@PropertyName("StoreID")
public void setStoreID(String StoreID) {
this.storeID = StoreID;
}
Also see: