com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.csaminorproject.www.procompete.pojo.Category类型

时间:2017-04-25 19:22:40

标签: android firebase firebase-realtime-database

我正在尝试在RecyclerView中显示firebase数据库中的stroed类别,但是我收到此错误

我的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();
}

}

我的数据库的屏幕截图是:

enter image description here

2 个答案:

答案 0 :(得分:0)

解决了问题 我试图引用错误的节点 要传递给FirebaseRecyclerAdapter的引用应该是mCategoryReference = FirebaseDatabase.getInstance()。getReference();而不是mCategoryReference = FirebaseDatabase.getInstance()。getReference()。child(CATEGORIES);

答案 1 :(得分:0)

我在类似的“用户”节点上遇到了同样的问题。当我使用正确的引用(子节点的子节点)时,此问题就解决了。

此引用导致错误:

databaseReferenceOfFirebase = FirebaseDatabase.getInstance().getReference()
    .child("users");

此人指出了正确的位置:

databaseReferenceOfFirebase = FirebaseDatabase.getInstance().getReference()
    .child("users")
    .child("TtrYYdL5OQT7bF5fJaPDuEsbqj83");