Skip to content
Advertisement

Main Adapter Cannot be Applied to Java List

(list , this)<= this Line Creates Problem.

This is my Main Acitivty Code

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import com.example.foodorder.Adapters.MainAdapter;
import com.example.foodorder.Models.MainModel;
import com.example.foodorder.databinding.ActivityMainBinding;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ArrayList<MainModel> list = new ArrayList<>();
        list.add(new MainModel(R.drawable.tabba, "TableA", "2500", "Unique Table for Interior Lovers"));
        list.add(new MainModel(R.drawable.tabbb, "TableB", "3500", "Unique Table for Interior Lovers"));
        list.add(new MainModel(R.drawable.tabbd, "TableC", "4500", "Unique Table for Interior Lovers"));
        list.add(new MainModel(R.drawable.tabbe, "TableD", "55500", "Unique Table for Interior Lovers"));
        MainAdapter adapter = new MainAdapter(list , this);
        binding.recyclerview.setAdapter(adapter);

    }
}

Main Adapter Code

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.viewholder> {
    ArrayList<MainModel> list;
    Context context;
    @NonNull
    @Override
    public viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.sample_mainfood,parent,false);
        return new viewholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull viewholder holder, int position) {
        final MainModel model =list.get(position);
        holder.mainName.setText(model.getName());
        holder.imageView.setImageResource(model.getImage());
        holder.price.setText(model.getPrice());
        holder.description.setText(model.getDescription());

    }

    @Override
    public int getItemCount() {

        return list.size();
    }

    public class viewholder extends RecyclerView.ViewHolder{

        ImageView imageView;
        TextView mainName, price, description;

        public viewholder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            imageView = itemView.findViewById(R.id.name);
            imageView = itemView.findViewById(R.id.orderPrice);
            imageView = itemView.findViewById(R.id.description);

        }
    }
}

This one is Main Model

package com.example.foodorder.Models;

public class MainModel {
    int image;
    String name, price, description;

    public MainModel(int image, String name, String price, String description) {
        this.image = image;
        this.name = name;
        this.price = price;
        this.description = description;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

Advertisement

Answer

Here, In your code, you are missing a constructor as mentioned by @Sergey as well as you are passing the wrong context in onCreateViewHolder().You should pass parent.getContext();

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.viewholder> {
ArrayList<MainModel> list;
Context context;

MainAdapter(ArrayList<MainModel> data, Context ctx) {
    list = data;
    context = ctx;
}

@NonNull
@Override
public viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_mainfood,parent,false);
    return new viewholder(view);
}
//...
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement