我有GridView
从SD卡(如果有)加载图像,否则会加载一些默认图像。加载图像后,用户可以选择任何图像单元并拍摄新照片。当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);
}
}
}
...
}
答案 0 :(得分:1)
你需要用新图片替换列表f中的图像,然后使用f中的项目来调用notify,adapter for diplaying