公共类juiceAdapterGreens扩展了RecyclerView.Adapter { AppCompatActivity appCompatActivity;
#define MY_MACRO(var) \
int var
private List<Greens> juiceList;
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView header,quantity,price,pieces;
public ImageView juicebottle;
public MyViewHolder(View view) {
super(view);
header = (TextView) view.findViewById(R.id.headergreens);
quantity = (TextView) view.findViewById(R.id.quantitygreens);
price = (TextView) view.findViewById(R.id.pricegreens);
pieces = (TextView) view.findViewById(R.id.piecesgreens);
juicebottle=(ImageView)view.findViewById(R.id.juicebottlegreens);
}
}
public juiceAdapterGreens(Context context, List<Greens> juiceList) {
this.juiceList = juiceList;
}
@Override
public juiceAdapterGreens.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.home_greens, parent, false);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DummyFragement dummyFragement = new DummyFragement ();
Context context=null;
((MainActivity)context).getSupportFragmentManager().beginTransaction().replace(R.id.container, dummyFragement,"OptionsFragment").addToBackStack(null).commit();
Fragment f1=Profile_Frag.newInstance("","");
replace(f1);
}
});
return new juiceAdapterGreens.MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(juiceAdapterGreens.MyViewHolder holder, int position) {
Greens juice = juiceList.get(position);
holder.header.setText(juice.getTitle());
holder.quantity.setText(juice.getQuantity());
holder.price.setText(juice.getPrice());
holder.pieces.setText(juice.getPieces());
holder.juicebottle.setImageResource(juice.getJuicebottle());
}
@Override
public int getItemCount() {
return juiceList.size();
}
private void replace(Fragment f1) {
FragmentManager fm=this.appCompatActivity.getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.frame_content_main,f1);
ft.commit();
}
}
以红色显示,我只需要使用上面输入的适配器类来加载片段。请帮我。
getSupportFragmentManager()
答案 0 :(得分:0)
您在context
回调中将null
定义为OnClick
:
Context context=null;
这就是为什么它会抛出NullPointerException
要使此行为无效,请在activityContext
类内声明变量MyViewHolder
并将变量context
分配为
public juiceAdapterGreens(Context context, List<Greens> juiceList) {
...
activityContext = context;
}
然后您可以使用
context.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, dummyFragement,"OptionsFragment")
.addToBackStack(null).commit();