ThingsILove1996 ThingsILove1996 - 12 days ago 7
Android Question

Cannot resolve getActivity() in recyclerView.adapter

Got stuck little bit. I tried change getActivity to 'c' (context), but then getSupportFragmentManager gets the same error. Read different topics on this, but couldn't find a solution. Checked libraries and tried to change them but nothing. Would appreciate some help!

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.mrti.menu.Fragments.MealsListFragment;
import com.example.mrti.menu.R;
import java.util.ArrayList;


public class CategoryAdapter extends RecyclerView.Adapter<ViewHolder> {

Context c;
ArrayList<Category> Categories;

public CategoryAdapter(Context c, ArrayList<Category> Categories) {
this.c = c;
this.Categories = Categories;
}
// INITIALIZE HOLDER
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.rec_item, null);
ViewHolder holder = new ViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {

// BIND DATA TO VIEWS

holder.mNameTextView.setText(Categories.get(position).getName());

// LOAD IMAGES FROM WEB AND CACHE THEM

Glide.with(c)
.load(Categories.get(position).getImage())
.diskCacheStrategy(DiskCacheStrategy.ALL) // Saves the media item after all transformations to cache and
// Saves just the original data to cache.
.placeholder(R.mipmap.ic_launcher)
.into(holder.img);

// LISTENER

holder.setItemClickListener(new itemClickListener() {
@Override
public void onItemClick(View v, int pos) {
Toast.makeText(c, Categories.get(pos).getName(), Toast.LENGTH_SHORT ).show();

MealsListFragment fragment = new MealsListFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.root_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();


}
});
}

Answer

Cannot resolve getActivity() in fragment

Your Java class is declared like this:

public class CategoryAdapter extends RecyclerView.Adapter<ViewHolder>

CategoryAdapter is not a fragment. CategoryAdapter is a RecyclerView.Adapter.

RecyclerView.Adapter does not have a getActivity() method. Hence, your code will not compile.

You seem to want to execute a FragmentTransaction inside the RecyclerView.Adapter. However, to be able to call getSupportFragmentManager(), you need the FragmentActivity that is hosting the RecyclerView to which your CategoryAdapter is attached.

But, a RecyclerView.Adapter has no direct or indirect access to that FragmentActivity. So, you need to get the FragmentActivity into the CategoryAdapter.

Your current constructor is declared as:

public CategoryAdapter(Context c, ArrayList<Category> Categories)

My guess is that in your activity, you are calling new CategoryAdapter(this, someListOfcategories). If so, change your constructor to:

public CategoryAdapter(FragmentActivity c, ArrayList<Category> Categories)

and change your field to be a FragmentActivity. Then, you can change:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();

to:

FragmentManager fragmentManager = c.getSupportFragmentManager();
Comments