我不明白问题是什么,据我所说,一切正常。
日志:
com.google.firebase.database.databaseexception:无法在com.google.firebase.database.core.utilities.encoding.customclassmapper.convertbean(customclassmapper)上将java.lang.string类型的对象转换为com.hamidmadarati.mobilsorgulari.home.dashboard.ui.passengerecyclermodel类型。java:436)在com.google.firebase.database.core.utilities.encoding.customclassmapper.deserializetoclass(customclassmapper。java:232)在com.google.firebase.database.core.utilities.encoding.customclassmapper.converttocustomclass(customclassmapper。java:80)在com.google.firebase.database.datasnapshot.getvalue(datasnapshot。java:203)
碎片:
public class PassengerFragment extends Fragment {
private RecyclerView listVehiclesWeekDay;
private FirebaseRecyclerOptions<PassengerRecyclerModel> options;
private FirebaseRecyclerAdapter<PassengerRecyclerModel, PassengerMyViewHolderRecycler> adapter;
private DatabaseReference databaseReference;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_passenger, container,false);
listVehiclesWeekDay = view.findViewById(R.id.listVehiclesWeekDay);
listVehiclesWeekDay.setHasFixedSize(true);
listVehiclesWeekDay.setLayoutManager(new LinearLayoutManager(getActivity()));
databaseReference = FirebaseDatabase.getInstance().getReference().child("Travels").child("DaysOfTheWeekSimple");
options = new FirebaseRecyclerOptions.Builder<PassengerRecyclerModel>().setQuery(databaseReference, PassengerRecyclerModel.class).build();
adapter = new FirebaseRecyclerAdapter<PassengerRecyclerModel, PassengerMyViewHolderRecycler>(options){
@Override
protected void onBindViewHolder(@NonNull PassengerMyViewHolderRecycler holder, int position, @NonNull PassengerRecyclerModel model) {
if (model.getDay() != null){
holder.titleTextView.setText(model.getDay());
}
}
@Override
public PassengerMyViewHolderRecycler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerow, parent, false);
return new PassengerMyViewHolderRecycler(view);
}
};
adapter.startListening();
listVehiclesWeekDay.smoothScrollToPosition(0);
listVehiclesWeekDay.setAdapter(adapter);
adapter.notifyDataSetChanged();
return view;
}
}
班级:
public class PassengerRecyclerModel {
private String day; //name
public PassengerRecyclerModel() {
//Empty - for Firebase Database
}
// model
public PassengerRecyclerModel(String day) {
this.day = day;
}
//Getter and Setter
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
}
1条答案
按热度按时间twh00eeo1#
传递以下引用时:
与firebaserecycleroptions.builder的“setquety()”方法的“passengerrecyclermodel”类一起,意味着您要使用firebase ui库来显示以下对象中存在的所有“passengerrecyclermodel”对象:
这实际上是不可能的,因为在该引用下它只存在一个string类型的子级,因此出现以下错误:
要解决这个问题,您需要在“daysoftheweeksimple”节点下添加一个额外的级别。这可以使用databasereference的push()方法简单地实现。因此,当您在上述引用处添加对象时,请按如下方式添加:
这样,数据库架构应该如下所示:
获取数据的代码可能保持不变。
编辑:
根据您的意见:
不,事实上,我们不创建数据
如果您直接在firebase控制台中添加数据,您将无法使用push()。如果不希望以编程方式添加数据,则仍然需要额外的层级别。因此,在这种情况下,在“daysoftheweeksimple”节点和“day”属性之间,添加另一个至少带有日期或id的节点,如下所示:
问题是当它被获取时,firebase认为它是一个对象而没有将它转换为字符串
如果不“think”,则对象的类型为“passengerrecyclermodel”,应该在适配器中相应地读取。