替换图像时动态刷新网格视图. 通知数据集已更改() 不起作用



>我有一个从SD卡加载图像的GridView(如果存在),否则会加载一些默认图像。加载图像后,用户可以选择任何图像单元格并拍摄新照片。当默认图片加载到GridView并且用户拍摄照片时,GridView更新并显示图像 - 但如果用户再次单击单元格并拍摄新图片,则GridView不会更新。只有当我杀死应用程序并重新启动它时,才会出现新图片。

我已经尝试过notifyDataSetChanged()但是刷新图像时它不起作用。

这是代码:

public class Fragment1 extends SherlockFragment {
...
private Integer[] mThumbIds = { R.drawable.fr, R.drawable.siderelaxed3,
R.drawable.br, R.drawable.fdbth, R.drawable.bdb, R.drawable.bls,
R.drawable.fls, R.drawable.mm };

private ImageAdapter imageAdapter;
ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;
ImageLoader imageLoader;    
Context context;
boolean needToRefresh = true;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
date = util.getCurrentDate();
Bundle bundle = this.getArguments();
date = bundle.getString(MyCommons.SELECTED_DATE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mainView = inflater.inflate(R.layout.picturerecord, container,
false);
getFromSdcard();
Grid = (GridView) mainView
.findViewById(R.id.pictureRecordGrid);
imageAdapter = new ImageAdapter();
Grid.setAdapter(imageAdapter);
imageLoader = new ImageLoader(context, 4);
try {
ourClass = Class
.forName("cameraOpen");
ourIntent = new Intent(context, ourClass);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
poseGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
startActivityForResult(ourIntent, CAMERA_REQUEST);
poseSelected = position;
}
});     
return mainView;
}
public void getFromSdcard() {
f.clear();
for (int  i = 0; i < MyCommons.POSES_TO_LOAD.length; i++) {
String pose = MyCommons.pose_names[i];
String path = MyCommons.rootPath + File.separator + pose
+ File.separator + date;
File imgFile = new File(path);
if (imgFile.exists()) {
f.add(path);
Log.d(TAG, "image is present at:" + path);
} else {
f.add("loaddefault");
}
}
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return f.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (needToRefresh == true) {
convertView = null;
Log.d(TAG, "NEEDED REFRESH");
needToRefresh = false;
}
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.picturerecorddata,
null);
holder.imageview = (ImageView) convertView
.findViewById(R.id.pictureRecordImage);
holder.textview = (TextView) convertView.findViewById(R.id.pictureRecordText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();             
}
if (f.get(position).equals("loaddefault")) {
DefaultImageLoader defaultImages = new DefaultImageLoader(holder.imageview);
defaultImages.execute(position);
} else {
imageLoader.DisplayImage(f.get(position), holder.imageview, mThumbIds[position], position);
}
holder.textview.setText(MyCommons.pose_names[position]);
return convertView;
}
}
class ViewHolder {
ImageView imageview;
TextView textview;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
Bitmap photo = BitmapFactory.decodeByteArray(
data.getByteArrayExtra("BitmapImage"), 0,
data.getByteArrayExtra("BitmapImage").length);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

File root = new File(MyCommons.rootPath);
if (!root.exists()) {
root.mkdirs();
Log.d(TAG, "Created Progress pics directory.");
}
String poseName = MyCommons.pose_names[poseSelected];
File pose_dir = new File(root + File.separator + poseName);
if (!pose_dir.exists()) {
pose_dir.mkdirs();
Log.d(TAG, "Created" + pose_dir + " pics directory.");
}
String tempFileName = root + File.separator + poseName + File.separator + date;
File tempFile = new File(tempFileName);
if (tempFile.exists()) {
Log.d(TAG, "File already exist. deleteing it and will write new file:" + tempFileName);
tempFile.delete();
}
util.saveImage(bytes, pose_dir, date);
Log.d(TAG, "image saved. Reloading adapter:n"  + tempFileName);
f.remove(tempFileName);
getFromSdcard();                
f.add(poseSelected, tempFileName);  
//f.clear();                
//f.addAll(f);              
imageAdapter.notifyDataSetChanged();
Grid.invalidateViews();
Grid.setAdapter(imageAdapter);                          
}
}
}
...

}

您需要将列表 f 中的图像替换为新图片,然后调用 notify,适配器仅使用 f 中的项目进行拆分

最新更新