Reputation: 133
While i am running the code below i am getting the adapter error,i tried solving it by seeing youtube videos ....still its not working ,its showing that adapter is abstract and so the error is there.which i have attached below.plz give the most relevent solution to this problem.
this is my GridAdapter.java code
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
List<String>titles;
List<Integer>images;
LayoutInflater inflater;
public GridAdapter(Context context,List<String>titles,List<Integer>images){
this.titles = titles;
this.images = images;
this.inflater = LayoutInflater.from(context);
}
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.intoduction,parent,false);
return new ViewHolder(view);
}
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.titles.setText(titles.get(position));
holder.images.setImageResource(images.get(position));
}
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView titles;
ImageView images;
public ViewHolder(@NonNull View itemView) {
super(itemView);
titles = itemView.findViewById(R.id.textView15);
images = itemView.findViewById(R.id.imageView4);
}
}
}
this is my MainActivity2.java code
public class MainActivity2 extends AppCompatActivity {
RecyclerView recycleview1;
List<String> titles;
List<Integer>images;
Adapter GridAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intropage);
recycleview1 = findViewById(R.id.recycleview1);
titles = new ArrayList<>();
images = new ArrayList<>();
titles.add("first item");
titles.add("second item");
titles.add("third item");
titles.add("fourth item");
images.add(R.drawable.bheeshma);
images.add(R.drawable.sadak2);
images.add(R.drawable.tanhaji);
images.add(R.drawable.thappad);
GridAdapter = **new Adapter(this,titles,images);**
GridLayoutManager gridLayoutManager = new
GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
recycleview1.setLayoutManager(gridLayoutManager);
recycleview1.setAdapter(**GridAdapter**);
}
}
this is the error i am getting
Adapter is abstract; cannot be instantiated
GridAdapter = new Adapter(this,titles,images);
Upvotes: 2
Views: 76
Reputation: 168
You didn't give any vairiable name for GridAdapter class.That's why you are getting this error.
First you have to declare it globally like
GridAdapter gridAdapter;
and then you should instantiate inside you onCreate() method like
gridAdapter = new GridAdapter(this,titles,images);
this will be the solution for your problem.
Upvotes: 1
Reputation: 347
You are using class name as variable name and the adapter you are using is of type GridAdapter(not Adapter). Make these changes in MainActivity2 file.
GridAdapter adapter;
adapter=new GridAdapter(this,titles,images);
recycleview1.setAdapter(adapter);
Upvotes: 1