在ListView上通过URI重新加载ImageView

时间:2015-11-15 17:52:50

标签: android android-listview android-asynctask

我有一个由ListView组成的ImageView元素。我使用AsyncTask获取了新图片,并且在onPostExecute(Object result)方法中,我使用setImageUri(Uri uri)设置了图片,但它没有更新。

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

我尝试使用invalidate(),扩展ImageView,父BaseAdapter的所有组合来调用ListView,但没有任何效果。我尝试过许多其他技巧,例如调用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?

2 个答案:

答案 0 :(得分:0)

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

adapter.notifyDataSetChanged();

答案 1 :(得分:0)

您可以这样做:

- 在asynctask调用中更改第三个参数。

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();
        }
    }
祝你好运!