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

时间:2016-11-23 22:56:03

标签: android android-fragments transactions

陷入困境。我尝试将getActivity更改为'c'(上下文),但getSupportFragmentManager获得相同的错误。阅读不同的主题,但找不到解决方案。检查过库并尝试更改它们但没有。希望得到一些帮助!

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


        }
    });
   }

2 个答案:

答案 0 :(得分:7)

  

无法解析片段

中的getActivity()

您的Java类声明如下:

public class CategoryAdapter extends RecyclerView.Adapter<ViewHolder>

CategoryAdapter不是片段。 CategoryAdapterRecyclerView.Adapter

RecyclerView.Adapter没有getActivity()方法。因此,您的代码将无法编译。

您似乎想在FragmentTransaction内执行RecyclerView.Adapter。但是,为了能够致电getSupportFragmentManager(),您需要托管FragmentActivity所附加的RecyclerView的{​​{1}}。

但是,CategoryAdapter没有对RecyclerView.Adapter的直接或间接访问权限。因此,您需要将FragmentActivity纳入FragmentActivity

您当前的构造函数声明为:

CategoryAdapter

我的猜测是,在您的活动中,您正在呼叫public CategoryAdapter(Context c, ArrayList<Category> Categories) 。如果是这样,请将构造函数更改为:

new CategoryAdapter(this, someListOfcategories)

并将您的字段更改为public CategoryAdapter(FragmentActivity c, ArrayList<Category> Categories) 。然后,您可以更改:

FragmentActivity

为:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();

答案 1 :(得分:1)

Context没有getActivity方法,只有Fragment方法。

因此,如果您想以这种方式使用它,则需要将字段从Context c;更改为Fragment c;,然后在c.getActivity().getSupportFragmentManager()中调用onItemClick。< / p>

或者,将您的字段更改为Activity c;,然后调用c.getSupportFragmentManager()