【问题标题】:setting image to imageview dynamically in listview item doesn't work properly在 listview 项中动态将图像设置为 imageview 无法正常工作
【发布时间】:2013-07-12 19:13:11
【问题描述】:

我有一个 listview,其中每个列表项都有一些 imageview 和一些 textview。我正在使用意图打开 gallery,选择一个选定的图像并将其分配给 imageview,但我的问题是,当我滚动列表视图时,当图像设置为该 imageview 时,选定的图像 消失,原始图像出现在那个地方。 那么谁能告诉我为什么会这样,我该如何避免呢?

Intent photoPickerIntent = new Intent(
                                    Intent.ACTION_PICK);
                            photoPickerIntent.setType("image/*");
                            startActivityForResult(photoPickerIntent,
@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch (requestCode) {
    case SELECT_PHOTO:
        if (resultCode == RESULT_OK) {
            Uri selectedImage = imageReturnedIntent.getData();
            /*
             * InputStream imageStream=null; try { imageStream =
             * getContentResolver().openInputStream(selectedImage); } catch
             * (FileNotFoundException e) { // TODO Auto-generated catch
             * block e.printStackTrace(); } Bitmap image =
             * BitmapFactory.decodeStream(imageStream);
             */
            try {
                LinearLayout ll = (LinearLayout) lv.getChildAt(position);
                ImageView im = (ImageView) ll
                        .findViewById(R.id.image);
                Bitmap b = decodeUri(selectedImage); 
                Bitmap circleBitmap = Bitmap.createBitmap(b.getWidth(),
                        b.getHeight(), Bitmap.Config.ARGB_8888);
                BitmapShader shader = new BitmapShader(b, TileMode.CLAMP,
                        TileMode.CLAMP);
                Paint paint = new Paint();
                paint.setShader(shader);
                Canvas c = new Canvas(circleBitmap);
                c.drawCircle(b.getWidth() / 2, b.getHeight() / 2,
                        b.getWidth() / 2, paint);
                im.setImageBitmap(circleBitmap);

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }                                   SELECT_PHOTO);


private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
// Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(
                getContentResolver().openInputStream(selectedImage), null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 40;

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(
                getContentResolver().openInputStream(selectedImage), null, o2);

    }

【问题讨论】:

  • 你能发布你到目前为止所做的代码吗??
  • @Selvin 非常有趣,如果您不想更好地提供帮助,请不要发表评论。
  • @Shubham 是的,这很有趣,如果你不想得到有趣的 cmets,最好不要发布不清楚的问题......

标签: android listview android-intent scroll imageview


【解决方案1】:

您应该更新您的 listadapter 或您的 listadapter 的来源。适配器是您的数据的模型。当您在适配器上调用 notifyDatasetChanged 时,您的视图将自动更新,并使用新值对其进行更新。

【讨论】:

  • 所选图像替换原始图像,但在滚动时再次更改。在那之后我没有添加任何东西,那么 notifyDataSetChanged 有什么帮助?
  • 正如我所说,适配器是您数​​据的模型,列表中的项目是适配器中记录的视图。因此,您更新模型而不是视图,当您更新模型并调用 notifyDatasetChanged() 时,您的视图将自动更新;一条经验法则:永远不要手动更新列表视图中的视图,仅在 ListView.getView() 中。数据集在适配器中失效后调用 ListView.getView()
  • 实际上我已经设置了该图像视图供用户单击,然后将他自己的图片粘贴到该图像视图中,因此我无法在 Listview.getView() 方法中执行此操作。
  • 是的,你可以,你应该这样做。你能显示适配器的代码吗?
猜你喜欢
  • 2011-12-10
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多