gpt4 book ai didi

android - 替换图像时动态刷新 gridview。 notifyDataSetChanged() 不工作

转载 作者:行者123 更新时间:2023-11-30 03:02:53 27 4
gpt4 key购买 nike

我有一个 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);
}
}
}
...

最佳答案

你需要用新图片替换列表f中的一张图片,然后调用通知,适配器只使用f中的项目进行显示

关于android - 替换图像时动态刷新 gridview。 notifyDataSetChanged() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22288651/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com