我该怎么办?这是GridView适配器
中的方法 public static void changeView(Bitmap bmp, int pos){
GridView gridView = new GridView(mContext);
ImageView view = (ImageView) gridView.getChildAt(pos);
if (view == null)
Log.e("ImageAdapter Error", "ImageView is null");
}
这是一个getView方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// imageView = Inflater.inflate(R.layout.image_layout, null);
imageView = new ImageView(mContext);
if (DefinedValues.width/3 < DefinedValues.height/4)
imageView.setLayoutParams(new GridView.LayoutParams
(DefinedValues.height/4, DefinedValues.height/4));
else
imageView.setLayoutParams(new GridView.LayoutParams
(DefinedValues.width/3-5, DefinedValues.width/3-5));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(R.drawable.test3);
return imageView;
}
每次imageView都为null。是否可以在类static中设置Context对象mContext?
这是一个将适配器附加到gridView
的片段类 public class fragment1 extends Fragment {
/* (non-Javadoc)
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
*/
GridView gridView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
//
Bundle savedInstanceState) {
if (container == null) {
Log.e("Null container", "Null");
return null;
}
Log.e("fragment1", "fragment1 reached!");
View view = inflater.inflate(R.layout.menu1_fragment, container, false);
gridView = (GridView)view.findViewById(R.id.gridview1);
gridView.setAdapter(new ImageAdapter(this.getActivity()));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageView imageView = new ImageView(getActivity());
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams
(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(vp);
imageView.setImageBitmap(DefinedValues.imageContainer.get(position).getThumbnail());
getActivity().addContentView(imageView, vp);
}
});
return gridView;
}
}
MyImage类: public void downloadThumbnail(){
DefinedValues.thumbnail.put("activity", DefinedValues.images);
DefinedValues.thumbnail.put("type", "thumbnail");
DefinedValues.thumbnail.put("name", this.name);
DefinedValues.thumbnail.put("width", DefinedValues.width + "");
//Creates request to server
String req = Json.stringToJson(DefinedValues.thumbnail);
Log.d(TAG, "downloadImage() called. Request to server: " + req);
DefinedValues.thumbnail.clear();
thumb = new ImageDownloader();
thumb.setListener(MyImage.this);
thumb.execute(req);
Log.d(TAG, "downloaded thumbnail nr." + imageCount);
}
public void returnImage(Bitmap res) {
Log.d(TAG, "Setting up thumbnail...");
this.setThumbnail(res);
}
答案 0 :(得分:0)
在适配器中创建一个集合来保存您需要应用的图像。当您需要更改特定getView
行的图像时,请更改相应集合中的图像:
private Bitmap[] imgCollection;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// imageView = Inflater.inflate(R.layout.image_layout, null);
imageView = new ImageView(mContext);
if (DefinedValues.width/3 < DefinedValues.height/4)
imageView.setLayoutParams(new GridView.LayoutParams
(DefinedValues.height/4, DefinedValues.height/4));
else
imageView.setLayoutParams(new GridView.LayoutParams
(DefinedValues.width/3-5, DefinedValues.width/3-5));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(imgCollection[position]); // <-- changed
return imageView;
}
public void changeImageBitmap (Bitmap bmp, int pos) {
imgCollection[pos] = bmp;
notifyDataSetChanged(); // refresh the listview
}
此外,如果您不想将冗余位图添加到集合中,只是为了将它们作为默认值加载,您可以使用getView
来管理它:
if (imgCollection[position] == null) {
imageView.setImageResource(R.drawable.test3);
} else {
imageView.setImageBitmap(imgCollection[position]); // <-- changed
}
可以在类static中设置Context对象mContext吗?
绝对不是。永远不应该这样做。如果上下文是静态的,它可能会阻止您的活动或其中的任何内容被垃圾收集。您的应用可能最终因资源不足而崩溃。