我有一个小问题,我确信这个问题很容易解决,但我很挣扎。我需要将ImageAdapter类中的资源解析为Gallery类。 ImageAdapter:
package rs.srecnaplaneta.www.srecnaplaneta;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.slika21,
R.drawable.slika22,
R.drawable.slika23,
R.drawable.slika24,
R.drawable.slika25,
R.drawable.slika26,
R.drawable.slika27,
R.drawable.slika28,
R.drawable.slika29
};
}
画廊:
package rs.srecnaplaneta.www.srecnaplaneta;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
public class klasaGalerija extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab3galerija, container, false);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GridView gridView = getView().findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
}
所以,问题是,在Gallery类中,在代码行中:
gridView.setAdapter(new ImageAdapter(this));
&#34;这&#34;不起作用。 Android Studio的自动解决方案是改变方法的第一个参数&#34; ImageAdapter&#34;来自&#34; Context&#34;到了&#34; klasaGalerija&#34;,但我不能这样做。它必须保持为&#34; Context&#34;我假设为了工作,但我不能指出它。
如果有人可以提供帮助,我会非常感激。
答案 0 :(得分:0)
因为它是一个片段,你必须使用getContext()代替这个,我想。