在ListView上按URI重新加载ImageView



我有一个由ImageView组成的元素的ListView。我使用AsyncTask获得了一个新映像,在onPostExecute(Object result)方法中,我使用setImageUri(Uri-Uri)设置了映像,但它不会更新。

如果我改变活动或在应用程序之间切换,图像会完美显示,但我想立即显示图像。

我试着用ImageView、扩展BaseAdapter和父ListView的所有组合调用invalidate(),但都没有成功。我尝试了许多其他技术,比如调用setImageResource(0)setImageUri(null),但没有结果。。。

编辑:

这里,部分代码:

public class ThingItemAdapter extends BaseAdapter {
    protected List<Thing> things;
    LayoutInflater inflater;
    public ThingItemAdapter(Context context, List<Thing> things) {
        this.things = things;
        this.inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return things.size();
    }
    @Override
    public Thing getItem(int position) {
        return things.get(position);
    }
    @Override
    public long getItemId(int position) {
        return things.get(position).getId();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int pos = position;
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = this.inflater.inflate(R.layout.thing_list_item, parent, false);
            holder.thingImageView = (ImageView) convertView.findViewById(R.id.thing_preview);
            holder.button = (ImageButton) convertView.findViewById(R.id.apply_button);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Thing thing = things.get(position);
        final long thingId = thing.getId();
        final Uri thingUri = thing.getPicture();
        holder.thingImageView.setImageURI(thingUri);
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // generate new file
                final TypedFile typedFile = new TypedFile("multipart/form-data", new File(thingUri.getPath()));
                new ReadAndStorePictureTask()
                        .execute(new Object[] { typedFile, holder.thingImageView, thing });
            }
        });
        // item detailed view listener
        holder.thingImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent((ThingApplication) ThingApplication.getContext(), ThingActivity.class);
                intent.putExtra(ThingActivity.EXTRA_THING_ID, thingId);
                context.startActivity(intent);
            }
        });
        return convertView;
    }
    private class ViewHolder {
        ImageView thingImageView;
        ImageButton button;
    }
    private class ReadAndStorePictureTask extends AsyncTask<Object, Void, Void> {
        ImageView imageView;
        Thing thing;
        ViewGroup parent;
        protected Void doInBackground(Object... params) {
            final TypedFile typedFile = (TypedFile) params[0];
            imageView = (ImageView) params[1];
            thing = (Thing) params[2];
            ((ThingApplication) ThingApplication.getContext()).getClient().apply(typedFile,
                    new Callback<Response>() {
                        @Override
                        public void failure(RetrofitError error) {
                            ...
                        }
                        @Override
                        public void success(Response nothing, Response response) {
                            try {
                                byte[] bytes = ThingApplication.getBytesFromStream(response.getBody().in());
                                Uri newImageURI = Uri.parse("uri://valid_uri"); // whatever, it exists in real code
                                thing.setPicture(newImageURI);
                                File file = ((ThingApplication) ThingApplication.getContext())
                                        .getFileFromURI(newImageURI); // this method works
                                ThingApplication.saveBytesToFile(bytes, file.getAbsolutePath());
                                thingService.storeThing(thing);
                            } catch (Exception e) {
                                ...
                            }
                        }
                    });
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            imageView.setImageURI(thing.getPicture());
            // force redraw. FIXME not working          
            /*
            * ANSWER HERE, PLEASE
            */
        }
    }
}

如何在onPostExecute(Object result)方法中立即显示更新后的URI

onPostExecute更新链接到ListView适配器的图像列表,然后通过调用通知适配器您更改了列表中的项目

adapter.notifyDataSetChanged();

您可以这样做:

-更改异步任务调用中的第三个参数。

new ReadAndStorePictureTask().execute(
    new Object[] { typedFile, holder.thingImageView, pos });

-然后,修改asynctask中的列表项并刷新。

private class ReadAndStorePictureTask extends AsyncTask<Object, Void, Void> {
        ImageView imageView;
        int position;
        ViewGroup parent;
        protected Void doInBackground(Object... params) {
            final TypedFile typedFile = (TypedFile) params[0];
            imageView = (ImageView) params[1];
            position = (Integer) params[2];
            ((ThingApplication) ThingApplication.getContext()).getClient().apply(typedFile,
                    new Callback<Response>() {
                        @Override
                        public void failure(RetrofitError error) {
                            ...
                        }
                        @Override
                        public void success(Response nothing, Response response) {
                            try {
                                byte[] bytes = ThingApplication.getBytesFromStream(response.getBody().in());
                                Uri newImageURI = Uri.parse("uri://valid_uri"); // whatever, it exists in real code
                                things.get(position).setPicture(newImageURI);
                                File file = ((ThingApplication) ThingApplication.getContext())
                                        .getFileFromURI(newImageURI); // this method works
                                ThingApplication.saveBytesToFile(bytes, file.getAbsolutePath());
                                thingService.storeThing(things.get(position));
                            } catch (Exception e) {
                                ...
                            }
                        }
                    });
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            notifyDataSetChanged();
        }
    }

祝你好运!

最新更新