问题
How can I do it? This is the method in GridView adapter
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");
}
This is a getView method
@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;
}
The imageView is null everytime. And is it ok to set Context object mContext in class static?
This is a fragment class that attaches adapter to 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 class: 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);
}
回答1:
make a collection in your adapter to hold the images that you need applied. When you need to change the image of a specific getView row, change in the image in the corresponding collection:
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
}
moreover, if you're adverse to putting redundant bitmaps in a collection just to load them as a default value you could manage with this in getView instead:
if (imgCollection[position] == null) {
imageView.setImageResource(R.drawable.test3);
} else {
imageView.setImageBitmap(imgCollection[position]); // <-- changed
}
is it ok to set Context object mContext in class static?
No absolutely not. there should never be a need to do this. if the context is static, it could stop your activity or anything in it from being garbage collected. Your app might eventually crash from lack of resources.
来源:https://stackoverflow.com/questions/13991843/changing-gridviews-imageview-outside-of-the-adapter