问题描述
我试图使用Recyclerview从Documents类加载数据,但错误出现在logcat"W/Firestore:(21.1.1)[CustomClassMapper]:在类id.MuhammadRafi.StockCount上找不到文档名称的设置程序/字段.文档".顺便问一下,我的错在哪里?
I was trying to load data from Documents class using Recyclerview, but the error appear on logcat "W/Firestore: (21.1.1) [CustomClassMapper]: No setter/field for Document Name found on class id.MuhammadRafi.StockCount.Documents". By the way, where is my fault?
Documents.class:
Documents.class :
public class Documents extends DocumentID {
String documentName;
String documentDate;
String inspectorName;
String marketLocation;
public Documents() {
}
public Documents(String documentName, String documentDate, String inspectorName, String marketLocation) {
this.documentName = documentName;
this.documentDate = documentDate;
this.inspectorName = inspectorName;
this.marketLocation = marketLocation;
}
public String getDocumentName() {
return documentName;
}
public String getDocumentDate() {
return documentDate;
}
public String getInspectorName() {
return inspectorName;
}
public String getMarketLocation() {
return marketLocation;
}
public void setDocumentName(String documentName) {
this.documentName = documentName;
}
public void setDocumentDate(String documentDate) {
this.documentDate = documentDate;
}
public void setInspectorName(String inspectorName) {
this.inspectorName = inspectorName;
}
public void setMarketLocation(String marketLocation) {
this.marketLocation = marketLocation;
}
}
DocumentList.class:
DocumentList.class :
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class DocumentList extends RecyclerView.Adapter<DocumentList.ViewHolder> {
private List<Documents> documentsList;
private Context context;
public DocumentList(Context context, List<Documents> documentsList) {
this.documentsList = documentsList;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_document_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Documents adapterDocuments = documentsList.get(position);
holder.textViewDocumentName.setText(adapterDocuments.getDocumentName());
holder.textViewDate.setText(adapterDocuments.getDocumentDate());
holder.textViewInspector.setText(adapterDocuments.getInspectorName());
holder.textViewLocation.setText(adapterDocuments.getMarketLocation());
}
@Override
public int getItemCount() {
return documentsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewDocumentName, textViewLocation, textViewInspector, textViewDate;
public ViewHolder(View itemView) {
super(itemView);
textViewDocumentName = itemView.findViewById(R.id.textNameDocument);
textViewLocation = itemView.findViewById(R.id.textLocation);
textViewInspector = itemView.findViewById(R.id.textInspector);
textViewDate = itemView.findViewById(R.id.textDocumentDate);
}
}
}
StartCounting.class:
StartCounting.class :
public class StartCounting extends AppCompatActivity {
private DocumentList documentListAdapter;
private RecyclerView recyclerViewDocument;
private RecyclerView.LayoutManager layoutManager;
private FirebaseFirestore firebaseFirestore;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_counting);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
firebaseFirestore = FirebaseFirestore.getInstance();
documentsList = new ArrayList<>();
documentListAdapter = new DocumentList(getApplicationContext(), documentsList);
recyclerViewDocument = findViewById(R.id.recyclerViewDocument);
recyclerViewDocument.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerViewDocument.setLayoutManager(layoutManager);
recyclerViewDocument.setAdapter(documentListAdapter);
protected void onStart() {
super.onStart();
firebaseFirestore.collection("Users").document(currentUser.getUid()).collection("Documents").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()) {
for(DocumentSnapshot documentSnapshot : task.getResult()) {
Documents documents = documentSnapshot.toObject(Documents.class);
documentsList.add(documents);
documentListAdapter.notifyDataSetChanged();
}
}
}
});
推荐答案
代码中的问题在于,Documents
类中的字段名称与数据库中的属性名称不同.您在Documents
类中有四个名为documentName
,documentDate
,inspectorName
,marketLocation
的字段,而在数据库中,我看到了不同的名称,Document Name
,Document Date
,Inspector Name
和Market Location
,并且不正确.名称必须匹配.
The problem in your code lies in the fact that the name of the fields in your Documents
class are different than the name of the properties in your database. You have in your Documents
class four fields named documentName
, documentDate
, inspectorName
, marketLocation
while in the database I see that the names are different, Document Name
, Document Date
, Inspector Name
and Market Location
and this is not correct. The names must match.
您有两种解决方案.第一种方法是根据数据库中已经存在的名称更改Documents
类中的fied的名称,也可以像这样在getter前面使用注释:
You have two solutions. The first one would to change the name of your fieds in the Documents
class according to what it already exists in the database or you can use an annotation in front of the getters like this:
@PropertyName("Document Name")
public String getDocumentName() {
return documentName;
}
这篇关于W/Firestore:[CustomClassMapper]:Android类没有设置器/字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!