public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int position = getArguments().getInt("Position");
String category = new String();
if(position == 0){
category = "Burgers";
}
if(position == 1){
category = "Nuggets";
}
View view = inflater.inflate(R.layout.fragment_menu, container, false);
final ListView listView = view.findViewById(R.id.fragmentList);
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myRef = database.getReference("Category").child(category);
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Order> orders = new ArrayList<>();
for(DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()) {
Order order = uniqueKeySnapshot.getValue(Order.class);
orders.add(order);
}
CustomAdapter adapter = new CustomAdapter(orders, getActivity());
listView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
return view;
}
我想根据位置的不同在listView上显示不同的数据,它可以在第一个选项卡上显示我放置在firebase中的菜单,但是当我滚动这些选项卡时,会在下面显示此错误。
>com.google.firebase.database.DatabaseException:无法转换对象 类型为java.util.ArrayList的类型为com.example.eatfast.Model.Order
有没有办法解决这个问题?