作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 Recyclerview 从 Documents 类加载数据,但 logcat 上出现错误“W/Firestore: (21.1.1) [CustomClassMapper]: No setter/field for Document Name found on class id.MuhammadRafi.StockCount .文件”。话说我的错在哪里呢?
文档.类:
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;
}
}
文档列表.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);
}
}
}
开始计数.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
的字段> 在数据库中,我看到名称不同,文档名称
、文档日期
、检查员名称
和市场位置
,这是不正确的。名称必须匹配。
您有两种解决方案。第一个是根据数据库中已存在的内容更改 Documents
类中字段的名称,或者您可以在 getter 前面使用注释,如下所示:
@PropertyName("Document Name")
public String getDocumentName() {
return documentName;
}
关于java - 带 Firestore : [CustomClassMapper]: No setter/field for class Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58025961/
我是一名优秀的程序员,十分优秀!