关于java:WITH / Firestore:[自定义ClassMapper]:Android类没有设置器/字段

W/Firestore: [CustomClassMapper]: No setter/field for class Android

我试图使用Recyclerview从Documents类加载数据,但错误出现在logcat" W / Firestore:(21.1.1)[CustomClassMapper]:在类id.MuhammadRafi.StockCount上找不到文档名称的设置程序/字段。文档"。顺便问一下,我的错在哪里?

我的Firestore数据库

文档字段

Documents.class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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类中有四个名为documentNamedocumentDateinspectorNamemarketLocation的字段,而在数据库中,我看到名称是不同的,Document NameDocument DateInspector NameMarket Location,这是不正确的。名称必须匹配。

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

1
2
3
4
@PropertyName("Document Name")
public String getDocumentName() {
    return documentName;
}

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