Firestore Recycler不会在应用程序屏幕上显示任何结果,没有错误 [英] Firestore Recycler not showing any results on app screen with no errors
问题描述
我想使用具有2个输入的firebase制作一个琐事应用:Description和class.每当我运行该应用程序时,显示的屏幕都是空白(标题除外),没有明确的指示或错误可能表明该问题.这些是最富有成效的logCat日志区域:
I want to make a chore app using a firebase that has 2 inputs: Description and class. Whenever I run the app, the screen that shows is blank (except for the header) There is no clear indication or errors that might show the problem. These are the most fruitful logCat log areas:
2019-07-12 16:42:37.306 5300-5300/? W/Firestore: (20.1.0) [CustomClassMapper]: No setter/field for length found on class com.example.choretest.Chores
2019-07-12 16:42:37.306 5300-5300/? W/Firestore: (20.1.0) [CustomClassMapper]: No setter/field for description found on class com.example.choretest.Chores
2019-07-12 16:42:37.307 5300-5300/? W/Firestore: (20.1.0) [CustomClassMapper]: No setter/field for length found on class com.example.choretest.Chores
2019-07-12 16:42:37.307 5300-5300/? W/Firestore: (20.1.0) [CustomClassMapper]: No setter/field for description found on class com.example.choretest.Chores
2019-07-12 16:42:37.307 5300-5300/? W/Firestore: (20.1.0) [CustomClassMapper]: No setter/field for length found on class com.example.choretest.Chores
2019-07-12 16:42:37.308 5300-5300/? W/Firestore: (20.1.0) [CustomClassMapper]: No setter/field for description found on class com.example.choretest.Chores
2019-07-12 16:43:47.301 5300-5385/com.example.choretest V/FA: Inactivity, disconnecting from the service
由于没有其他线索,我尝试检查以确保所有馆藏和文档的名称都一致.
I have tried checking to make sure all the names of the collections and documents were consistent since I have no other leads.
主要活动:
private RecyclerView mMainList;
private static final String TAG = "MainActivity";
public FirebaseFirestore mFirestore;
private ChoresListAdapter ChoresListAdapter;
private List<Chores> choreList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMainList = (RecyclerView) findViewById(R.id.main_list);
mFirestore = FirebaseFirestore.getInstance();
choreList = new ArrayList<>();
ChoresListAdapter = new ChoresListAdapter(choreList);
mMainList.setHasFixedSize(true);
mMainList.setLayoutManager(new LinearLayoutManager(this));
mMainList.setAdapter((ChoresListAdapter));
mFirestore.collection("choreList").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if(e != null){
Log.d(TAG, "ERROR: " + e.getMessage());
}
for(DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()){
if(doc.getType() == DocumentChange.Type.ADDED){
Chores chores = doc.getDocument().toObject(Chores.class);
choreList.add(chores);
ChoresListAdapter.notifyDataSetChanged();
}
}
}
});
}
中学杂务课:
String chore, time;
public Chores(){
}
public Chores(String chore, String time) {
this.chore = chore;
this.time = time;
}
public String getChore() {
return chore;
}
public void setChore(String chore) {
this.chore = chore;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
recyclerview的适配器:
Adapter for the recyclerview:
public List<Chores> choreList;
public ChoresListAdapter(List<Chores> choreList){
this.choreList = choreList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.lengthText.setText(choreList.get(position).getTime());
holder.descriptionText.setText(choreList.get(position).getChore());
}
@Override
public int getItemCount() {
return choreList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
View mView;
public TextView descriptionText;
public TextView lengthText;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
descriptionText = (TextView) mView.findViewById(R.id.description_text);
lengthText = (TextView) mView.findViewById(R.id.length_text);
}
}
我希望输出是Firestore集合上消息的列表,但是,只有一个空白屏幕.
I expect the output to be a list of the messages on the firestore collection, but instead, there is only a blank screen.
推荐答案
问题是 Chores
类中的字段名称与数据库中存在的名称不同.为了解决这个问题,您应该将模型类中的 chore
和 time
的属性名称更改为 description
和 lenght
或您应该更改数据库中的名称以匹配模型类中的名称.
The problem is that the name of the fields in your Chores
class are different than the names that exist in the database. To solve this you should change the property names of chore
and time
in your model class to description
and lenght
or you should change the name in the database to match the names in your model class.
另一个解决方法可能是使用注释.您可以将您的字段设为 public
,并在每个字段的前面添加以下注释:
Another workaround might be to use annotations. You can make your fields public
and add in front of each fields the following annotations:
@PropertyName("description")
public String chore;
@PropertyName("lenght")
public String time
所以请记住,模型类中的所有fied名称必须与数据库中存在的fied名称匹配.
So remember, all the fieds names in your models class must match the one that exist in the database.
这篇关于Firestore Recycler不会在应用程序屏幕上显示任何结果,没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!