Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I am trying to implement RecyclerView and CardView and I am working with json. All my code is working fine and RecyclerView adapter is also not showing any error.

But in my main activity when i try to call adapter it is showing a error.

I took all the reference from the refrence link given below.

Foodlist.java :

<pre lang="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:

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


FoodViewHolder.java

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


MainActivity.java in onCreate i defined:

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


And I want to call my adapter in json below in another function:

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

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



Error is :

Wrong 1st argument type. Found 'com.gampzz.classes.Foodlist' required: 'android.widget.listadapter' 


What should i do? thanks in advance.

What I have tried:

http://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900