在实施RecyclerView和cardview时,适配器无法正常工作

时间:2016-06-01 10:41:48

标签: java android json android-recyclerview

我正在尝试实施RecyclerViewCardView,我正在与json合作。 我的所有代码都运行良好,RecyclerView适配器也没有显示任何错误。

但在我尝试调用适配器的主要活动中,它显示错误。

我从here

获取了所有引用

Foodlist.java

public class FoodList extends RecyclerView.Adapter<FoodList.FoodViewHolder>{

    private final Activity context;
    LayoutInflater inflater;


    public static class FoodViewHolder extends RecyclerView.ViewHolder {

        TextView txt1;
        TextView txt2;
        TextView txt3;
        TextView txt4;
        CardView cv;

        FoodViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            txt1 = (TextView) itemView.findViewById(R.id.txt1);
            txt2 = (TextView) itemView.findViewById(R.id.txt2);
            txt3 = (TextView) itemView.findViewById(R.id.txt3);
            txt4 = (TextView) itemView.findViewById(R.id.txt4);
        }
    }

    List<Food> food;
    public FoodList(Activity context, List<Food> food) {
        this.food = food;
        inflater = context.getLayoutInflater();
        this.context = context;
    }

    @Override
    public FoodViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.examplelistview, viewGroup, false);
        FoodViewHolder fvh = new FoodViewHolder(v);
        return fvh;
    }

    @Override
    public void onBindViewHolder(FoodViewHolder holder, int position) {
        View view;
        view = inflater.inflate(R.layout.examplelistview, null, true);
        if (view == null) {
            holder.txt1 = (TextView)view.findViewById(R.id.txt1);
            holder.txt2 = (TextView) view.findViewById(R.id.txt2);
            holder.txt3 = (TextView) view.findViewById(R.id.txt3);
            holder.txt4 = (TextView) view.findViewById(R.id.txt4);
            view.setTag(holder);
        }

        else {
            holder = (FoodViewHolder) view.getTag();
        }

        final Food item = food.get(position);
        holder.txt1.setText(item.txt1);
        holder.txt2.setText(item.txt2);
        holder.txt3.setText(item.txt3);
        holder.txt4.setText(item.txt4);
    }

    @Override
    public int getItemCount() {
        return food.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

}

Food.java:

public class Food {
    public String txt1, txt2, txt3, txt4;

}

FoodViewHolder.java

    public class FoodViewHolder {
    TextView txt1, txt2, txt3, txt4;
}

MainActivity.java

RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getApplicationContext());
rv.setLayoutManager(llm);

final FoodList adapter = new FoodList(MainActivity.this,food);
rv.setAdapter(adapter);

我想在另一个函数中在json下面调用我的适配器:

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setAdapter(adapter<here it is showing the error>, new DialogInterface.OnClickListener() {
}

我该怎么办? 在此先感谢

0 个答案:

没有答案