• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How do I get the categories class to transition to subcategories class

  • Thread starter Thread starter Alex8540
  • Start date Start date
A

Alex8540

Guest
Hi

I'm currently using fragments to create a category screen and ranking screen, however, I now want to add subcategories to the categories when clicked, however now upon clicking a category now I get this error

(Unable to instantiate activity ComponentInfo{com.example.educat/com.example.educat.SubCat}: java.lang.ClassCastException: com.example.educat.SubCat cannot be cast to android.app.Activity)

The categories class currently fetches the categories from firebase and lists them, I now want on click of a category to take the user to subcategories also fetched from firebase.

Any help would be appreciated.

public class CategoryFragment extends Fragment {

View myFragment;

RecyclerView listCategory;
RecyclerView.LayoutManager layoutManager;
FirebaseRecyclerAdapter<Category, CategoryViewHolder> adapter;
FirebaseDatabase database;
DatabaseReference categories;

public static CategoryFragment newInstance(){
CategoryFragment categoryFragment =new CategoryFragment();
return categoryFragment;
}
@override
public void onCreate(@nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
database= FirebaseDatabase.getInstance();
categories = database.getReference("Category");


}
@nullable
@override
public View onCreateView(@NonNull LayoutInflater inflater, @nullable ViewGroup container, @nullable Bundle savedInstanceState) {
myFragment = inflater.inflate(R.layout.fragment_category,container,false);
listCategory = (RecyclerView)myFragment.findViewById(R.id.listCategory);
listCategory.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(container.getContext());
listCategory.setLayoutManager(layoutManager);
loadCategories();

return myFragment;
}

private void loadCategories() {
adapter = new FirebaseRecyclerAdapter<Category, CategoryViewHolder>(
Category.class,
R.layout.category_layout,
CategoryViewHolder.class,
categories
) {
@override
protected void populateViewHolder(CategoryViewHolder viewHolder, final Category category, int position) {
viewHolder.category_name.setText(category.getName());
Picasso.with(getActivity())
.load(category.getImage())
.into(viewHolder.category_image);
viewHolder.setItemClickListener(new ItemClickListener() {
@override
public void onClick(View view, int position, boolean isLongClick) {
Intent startGame = new Intent(getActivity(),SubCat.class);
Common.categoryId = adapter.getRef(position).getKey();
Common.categoryName = category.getName();
startActivity(startGame);

}
});
}
};
adapter.notifyDataSetChanged();
listCategory.setAdapter(adapter);
}

public class SubCat extends Fragment {

View myFragment;

RecyclerView listCategory;
RecyclerView.LayoutManager layoutManager;
FirebaseRecyclerAdapter<Category, CategoryViewHolder> adapter;
FirebaseDatabase database;
DatabaseReference categories;

public static CategoryFragment newInstance(){
CategoryFragment categoryFragment =new CategoryFragment();
return categoryFragment;
}
@override
public void onCreate(@nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
database= FirebaseDatabase.getInstance();

categories = database.getReference("Subcategory");
}

@nullable
@override
public View onCreateView(@NonNull LayoutInflater inflater, @nullable ViewGroup container, @nullable Bundle savedInstanceState) {
myFragment = inflater.inflate(R.layout.fragment_category,container,false);
listCategory = (RecyclerView)myFragment.findViewById(R.id.listCategory);
listCategory.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(container.getContext());
listCategory.setLayoutManager(layoutManager);
loadCategories();

return myFragment;
}

private void loadCategories() {
adapter = new FirebaseRecyclerAdapter<Category, CategoryViewHolder>(
Category.class,
R.layout.category_layout,
CategoryViewHolder.class,
categories
) {
@override
protected void populateViewHolder(CategoryViewHolder viewHolder, final Category category, int position) {
viewHolder.category_name.setText(category.getName());
Picasso.with(getActivity())
.load(category.getImage())
.into(viewHolder.category_image);
viewHolder.setItemClickListener(new ItemClickListener() {
@override
public void onClick(View view, int position, boolean isLongClick) {
//Toast.makeText(getActivity(),String.format("%s|%s",adapter.getRef(position).getKey(),category.getName()),Toast.LENGTH_SHORT).show();
Intent startGame = new Intent(getActivity(),Start.class);
Common.categoryId = adapter.getRef(position).getKey();
Common.categoryName = category.getName();
startActivity(startGame);

}
});
}
};
adapter.notifyDataSetChanged();
listCategory.setAdapter(adapter);
}
 
Back
Top Bottom