Я пытаюсь прикрепить данные в поле зрения. Но я здесь показываю ошибку при OnBind
методе.
Вот код mainActivity
:
package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
int [] image = {
R.drawable.deep_blue,R.drawable.dual_matter,R.drawable.gradiant,
R.drawable.light_blue,R.drawable.yellow,R.drawable.red
};
MyAdapter myAdapter;
String [] title,desc;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.RecyclerViewID);
title=getResources().getStringArray(R.array.logo_name);
desc=getResources().getStringArray(R.array.logo_desc);
myAdapter =new MyAdapter(this,title,desc,image);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
Вот код Adapter
класса:
package com.example.recyclerview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyAdapterHolder> {
Context context;
String [] title;
String [] desc;
int [] image;
public MyAdapter(Context context, String[] title, String[] desc, int[] image) {
this.context = context;
this.title = title;
this.desc = desc;
this.image = image;
}
@NonNull
@Override
public MyAdapterHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view=layoutInflater.inflate(R.layout.sampleview,viewGroup,false);
return new MyAdapterHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyAdapterHolder myAdapterHolder, int position) {
myAdapterHolder.titleTV.setText(title[i]);
myAdapterHolder.descTV.setText(desc[i]);
myAdapterHolder.myimageView.setImageResource(image[i]);
}
@Override
public int getItemCount() {
return title.length;
}
public class MyAdapterHolder extends RecyclerView.ViewHolder {
TextView titleTV,descTV;
ImageView myimageView;
public MyAdapterHolder(@NonNull View itemView) {
super(itemView);
titleTV=itemView.findViewById(R.id.titleTextViewID);
descTV=itemView.findViewById(R.id.descTextViewID);
myimageView=itemView.findViewById(R.id.myImageViewID);
}
}
}