简体   繁体   中英

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();


        }
    });
   }

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();

Context does not have a getActivity method, only Fragment does.

So, if you want to use it in this way, you need to change your field from Context c; into Fragment c; , and then call c.getActivity().getSupportFragmentManager() in onItemClick .

Or alternatively, change your field to Activity c; , and then call c.getSupportFragmentManager() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM