0
我試圖顯示在firebase數據庫中stroed的類別一個RecyclerView但我得到這個錯誤com.google.firebase.database.DatabaseException:無法將java.lang.String類型的對象轉換爲類型com.csaminorproject.www.procompete.pojo.Category
我的POJO如下:
package com.csaminorproject.www.procompete.pojo;
import java.util.ArrayList;
import java.util.List;
public class Category {
List<String> categories = new ArrayList<>();
public Category() {
}
public Category(List<String> categories) {
this.categories = categories;
}
public List<String> getCategories() {
return categories;
}
public void setCategories(List<String> categories) {
this.categories = categories;
}
}
具有這種RecyclerView的片段是:
public class SelectCategoryFragment extends Fragment {
static AppBarLayout appBar;
static LinearLayout selectCategoryFragment;
private static final String CATEGORIES = "categories";
private DatabaseReference mCategoryReference;
private FirebaseRecyclerAdapter mFirebaseAdapter;
private RecyclerView mCategoryRecyclerView;
public SelectCategoryFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_select_category, container, false);
appBar = (AppBarLayout) getActivity().findViewById(R.id.appBar);
selectCategoryFragment = (LinearLayout) getActivity().findViewById(R.id.selectCategoryFragment);
ImageView hideFilterFragment = (ImageView) view.findViewById(R.id.hideSelectCategoryFragment);
hideFilterFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appBar.setVisibility(View.VISIBLE);
selectCategoryFragment.setVisibility(View.GONE);
}
});
mCategoryRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_selectCategory);
mCategoryReference = FirebaseDatabase.getInstance().getReference().child(CATEGORIES);
Category c = new Category();
setUpFirebaseAdapter();
return view;
}
private void setUpFirebaseAdapter() {
mFirebaseAdapter = new FirebaseRecyclerAdapter<Category,
FirebaseCategoryViewHolder>(
Category.class,
R.layout.category_list_item,
FirebaseCategoryViewHolder.class,
mCategoryReference) {
@Override
protected void populateViewHolder(FirebaseCategoryViewHolder viewHolder,
Category model, int position) {
viewHolder.bindCategory(model);
}
};
mCategoryRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mCategoryRecyclerView.setAdapter(mFirebaseAdapter);
mCategoryRecyclerView.setHasFixedSize(true);
}
@Override
public void onDestroy() {
super.onDestroy();
mFirebaseAdapter.cleanup();
}
}
我的數據庫的截圖: