简体   繁体   English

无法在recyclerView.adapter中解析getActivity()

[英]Cannot resolve getActivity() in recyclerView.adapter

Got stuck little bit. 卡住了一点点。 I tried change getActivity to 'c' (context), but then getSupportFragmentManager gets the same error. 我尝试将getActivity更改为'c'(上下文),但getSupportFragmentManager获得相同的错误。 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 无法解析片段中的getActivity()

Your Java class is declared like this: 您的Java类声明如下:

public class CategoryAdapter extends RecyclerView.Adapter<ViewHolder>

CategoryAdapter is not a fragment. CategoryAdapter不是片段。 CategoryAdapter is a RecyclerView.Adapter . CategoryAdapterRecyclerView.Adapter

RecyclerView.Adapter does not have a getActivity() method. RecyclerView.Adapter没有getActivity()方法。 Hence, your code will not compile. 因此,您的代码将无法编译。

You seem to want to execute a FragmentTransaction inside the RecyclerView.Adapter . 您似乎想要在RecyclerView.Adapter执行FragmentTransaction However, to be able to call getSupportFragmentManager() , you need the FragmentActivity that is hosting the RecyclerView to which your CategoryAdapter is attached. 但是,为了能够调用getSupportFragmentManager() ,您需要托管与CategoryAdapter相连的RecyclerViewFragmentActivity

But, a RecyclerView.Adapter has no direct or indirect access to that FragmentActivity . 但是, RecyclerView.Adapter没有对FragmentActivity直接或间接访问权限。 So, you need to get the FragmentActivity into the CategoryAdapter . 因此,您需要将FragmentActivity放入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) . 我的猜测是,在你的活动中,你正在调用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 . 并将您的字段更改为FragmentActivity Then, you can change: 然后,您可以更改:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();

to: 至:

FragmentManager fragmentManager = c.getSupportFragmentManager();

Context does not have a getActivity method, only Fragment does. Context没有getActivity方法,只有Fragment才有。

So, if you want to use it in this way, you need to change your field from Context c; 所以,如果你想以这种方式使用它,你需要从Context c;改变你的字段Context c; into Fragment c; 进入Fragment c; , and then call c.getActivity().getSupportFragmentManager() in onItemClick . ,然后在onItemClick调用c.getActivity().getSupportFragmentManager()

Or alternatively, change your field to Activity c; 或者,将您的字段更改为Activity c; , and then call c.getSupportFragmentManager() . ,然后调用c.getSupportFragmentManager()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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