我有一个自定义的gridview适配器CoursesAdapter,该适配器正在与cardview一起使用,但是由于某些原因,从arraylist设置时,图像视图src无法正常工作。但是,当我直接将图像ID分配给imageView时,它就会起作用。
感谢您的帮助。
自定义GridView适配器
public class coursesAdapter extends BaseAdapter {
private Context context;
private ArrayList<Course> courseArrayList;
//constructor
public coursesAdapter(Context context, ArrayList<Course> courseArrayList) {
this.context = context;
this.courseArrayList = courseArrayList;
}
@Override
public int getCount() {
return courseArrayList.size();
}
@Override
public Course getItem(int position) {
return courseArrayList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Course course=courseArrayList.get(position);
LayoutInflater layoutInflater=LayoutInflater.from(context);
convertView=layoutInflater.inflate(R.layout.course_cardview,parent,false);
final ImageView courseImage=(ImageView) convertView.findViewById(R.id.course_image_imageView);
final TextView courseTitle = convertView.findViewById(R.id.course_title_textView);
//Here if I use courseImage.setImageResource(R.drawable.sample_thumbnail);
//image is shown
courseImage.setImageResource(courseArrayList.get(position).getImage());
courseTitle.setText(courseArrayList.get(position).getName());
return convertView;`enter code here`
}
}
由于此适配器用于测试,因此我用相同的值填充了数组列表。
适配器的阵列列表
for(int i=0;i<10;i++)
courseArrayList.add(new Course("Maths","Arslan",R.drawable.sample_thumbnail));
课程等级
public class Course implements Parcelable{
private String name; // course Name
private String lecturer; // course lecturer
private int image; //course Image
//constructors
public Course(String name, String lecturer, int image) {
this.name = name;
this.lecturer = lecturer;
this.image = image;
}
//Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLecturer() {
return lecturer;
}
public void setLecturer(String lecturer) {
this.lecturer = lecturer;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
// Parcelable Overridden functions
protected Course(Parcel in) {
name = in.readString();
lecturer = in.readString();
image = in.readInt();
}
public static final Creator<Course> CREATOR = new Creator<Course>() {
@Override
public Course createFromParcel(Parcel in) {
return new Course(in);
}
@Override
public Course[] newArray(int size) {
return new Course[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(lecturer);
dest.writeInt(image);
}
}
答案 0 :(得分:1)
更改此行:
courseImage.setImageResource(courseArrayList.get(position).getImage());
收件人:
courseImage.setImageResource(ContextCompat.getDrawable(context,courseArrayList.get(position).getImage()));
答案 1 :(得分:0)
尝试这种方式
添加此库
implementation 'com.squareup.picasso:picasso:2.71828'
并将其添加到您的适配器中
Picasso.get().load(courseArrayList.get(position).getImage()).into(imageView1);
让我知道是否可行
答案 2 :(得分:0)
添加此库
implementation 'com.github.bumptech.glide:glide:3.8.0'
以及您用于查看图片的代码
Glide.with(context).load(courseArrsylist.get(position).getImage())
.override(100, 100)// here your can use the size of image
.into(courseImage);