我有一个网格视图,在适配器中加载了6个单元格。当我点击每个单元格时,我将通过拍照或从图库中选择图像来添加图像。在选择图像后,网格视图仅显示为空。虽然我在一个单元格中设置图像,但是当转到另一个单元格时,之前的选择已经消失。如何完成它?请求帮助我。如果我有什么不对的地方,请指导我。
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.fpc_document_view, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
imageView = (ImageView) grid.findViewById(R.id.grid_image);
if (fileList.size() == 0) {
textView.setText(DOCUMENT_NAME_LIST[position].toString());
for (int i = 0; i <= 6; i++) {
imageView.setImageResource(R.mipmap.ic_add_document);
}
} else {
Bitmap bitmapResized = null;
for (int i = 0; i < fileList.size(); i++) {
if (!fileList.get(i).equals("")) {
System.out.println("fileList here ,,,," + fileList.get(i).toString());
Drawable drawable = mContext.getResources().getDrawable(R.mipmap.ic_add_document);
bitmapResized = ((BitmapDrawable) drawable).getBitmap();
} else {
Uri selectedImageUri = Uri.fromFile(fileList.get(i));
bitmapResized = ImageRelatedStuff.convertURIToBitmap(selectedImageUri, mContext);
if (bitmapResized != null) {
Bitmap bitmapTemp = bitmapResized;
bitmapResized = null;
bitmapResized = ImageRelatedStuff.getResizedBitmap(bitmapTemp, 500, 500, 0);
}
}
imageView.setImageBitmap(ImageRelatedStuff.getRoundedCornerBitmap(bitmapResized, 15));
}
}
} else {
grid = convertView;
imageView = (ImageView) grid.findViewById(R.id.grid_image);
}
答案 0 :(得分:2)
更改您的代码:
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.fpc_document_view, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
imageView = (ImageView) grid.findViewById(R.id.grid_image);
} else {
grid = convertView;
imageView = (ImageView) grid.findViewById(R.id.grid_image);
}
if (fileList.size() == 0) {
textView.setText(DOCUMENT_NAME_LIST[position].toString());
for (int i = 0; i <= 6; i++) {
imageView.setImageResource(R.mipmap.ic_add_document);
}
} else {
Bitmap bitmapResized = null;
for (int i = 0; i < fileList.size(); i++) {
if (!fileList.get(i).equals("")) {
System.out.println("fileList here ,,,," + fileList.get(i).toString());
Drawable drawable = mContext.getResources().getDrawable(R.mipmap.ic_add_document);
bitmapResized = ((BitmapDrawable) drawable).getBitmap();
} else {
Uri selectedImageUri = Uri.fromFile(fileList.get(i));
bitmapResized = ImageRelatedStuff.convertURIToBitmap(selectedImageUri, mContext);
if (bitmapResized != null) {
Bitmap bitmapTemp = bitmapResized;
bitmapResized = null;
bitmapResized = ImageRelatedStuff.getResizedBitmap(bitmapTemp, 500, 500, 0);
}
}
imageView.setImageBitmap(ImageRelatedStuff.getRoundedCornerBitmap(bitmapResized, 15));
}
}
问题是当你的convertView不等于null时,你没有在imageView上设置任何图像。这就是为什么在convertView为null的第二个单元格中,你在前一个非空的单元格上获得图像,你什么也得不到。
答案 1 :(得分:0)
public class MyAdapter extends BaseAdapter{
private final int GRID_COUNT = 6;
// you need an array save bitmap with position
Bitmap[] bitmapArray;
public MyAdapter(){
bitmapArray = new Bitmap[GRID_COUNT];
}
@Override
public int getCount() {
return 6;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
// inflater your layout
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.grid_image);
// get the bitmap in array by position
Bitmap bitmap = bitmapArray[position];
// when bitmap is null ,show default picture
if (bitmap == null){
imageView.setBackgroundResource(R.drawable.ic_default);
}else{
imageView.setImageBitmap(bitmap);
}
return convertView;
}
// activity or fragment use this method call adapter refresh
public void setBitmap(int position, Bitmap bitmap){
bitmapArray[position] = bitmap;
notifyDataSetChanged();
}
}
更新我的回答,希望能帮到你