【问题标题】:W/Firestore: [CustomClassMapper]: No setter/field for class AndroidW/Firestore:[CustomClassMapper]:Android 类没有设置器/字段
【发布时间】:2020-01-21 08:53:29
【问题描述】:

我尝试使用 Recyclerview 从 Documents 类加载数据,但错误出现在 logcat "W/Firestore: (21.1.1) [CustomClassMapper]: No setter/field for Document Name found on class id.MuhammadRafi.StockCount .文件”。对了,我的错在哪里?

My Firestore Database

Documents field

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 :

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 :

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 字段。您的 getter/setter 与数据库不匹配
  • 请向我们展示您在Documents 集合中的文档内容。也请回复@AlexMamo
  • 我已编辑,谢谢@Ashish
  • 我已编辑,谢谢@AlexMamo

标签: java android firebase android-recyclerview google-cloud-firestore


【解决方案1】:

代码中的问题在于Documents 类中的字段名称与数据库中属性的名称不同。您在Documents 类中有四个字段,分别名为documentNamedocumentDateinspectorNamemarketLocation,而在数据库中我看到名称不同,Document NameDocument DateInspector NameMarket Location 这是正确的。名称必须匹配。

您有两种解决方案。第一个是根据数据库中已经存在的内容更改 Documents 类中字段的名称,或者您可以在 getter 前面使用注释,如下所示:

@PropertyName("Document Name")
public String getDocumentName() {
    return documentName;
}

【讨论】:

    【解决方案2】:

    基本上,如果您想将您的文档转换为数据类,您的数据类字段应该与您的 Firestore 字段命名相同。 因此,如果在您的数据类中您有 documentName,那么您的 Firestore 应该有一个名为 documentName 的变量,而不是 Document Name

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-15
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 2018-01-24
      • 1970-01-01
      相关资源
      最近更新 更多