我需要制作我的adapter
显示图像,所以我创建了一个整数数组,并在其中添加了一个id的图像,但是当我将它发送到数组适配器构造函数时,它会给我一个错误,因为类型{ {1}}。我怎么能解决这个问题?
错误出现在这里超级(上下文,资源,对象);
我的GridViewAdapter类:
Integer
MyMainFragment类:
package com.example.hima.moviesapp;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import java.util.ArrayList;
/**
* Created by Hima on 3/23/2016.
*/
public class GridViewAdapter extends ArrayAdapter<String> {
private Context mContext;
private int layoutResourceId;
private ArrayList<Integer > mGridImages = new ArrayList<Integer>();
public GridViewAdapter(Context context, int resource, ArrayList<Integer> objects) {
//the error apear here!!
super(context,resource, objects);
this.mContext=context;
this.layoutResourceId=resource;
this.mGridImages=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageView image;
if(row == null){
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
image= (ImageView)row.findViewById(R.id.movieImage);
row.setTag(image);
}
else{
image = (ImageView)row.getTag();
}
int temp = mGridImages.get(position);
image.setImageResource(temp);
return row;
}
}
答案 0 :(得分:1)
您的适配器需要一系列字符串,这就是它崩溃的原因。 这样做是因为您在适配器中将String作为泛型类型传递
public class GridViewAdapter extends ArrayAdapter<String>
要修改您的适配器以扩展ArrayAdapter<Integer>
答案 1 :(得分:1)
尝试更改
public class GridViewAdapter extends ArrayAdapter<String> {
到
public class GridViewAdapter extends ArrayAdapter<Integer> {