简体   繁体   English

如何从自定义适配器动态设置列表视图的imageView源?

[英]How to dynamically set imageView source for a List View from Custom Adapter?

I'm trying to set an imageView source based on a string within a custom ArrayAdapter, but I'm unable to get it to work. 我正在尝试根据自定义ArrayAdapter中的字符串设置imageView源,但我无法让它工作。 I know the object rec_gift is coming through correctly and the icon_string variable get's the correct name, but the setImageResource call isn't working. 我知道对象rec_gift正确通过并且icon_string变量得到了正确的名称,但是setImageResource调用不起作用。

public class MySimpleArrayAdapter extends ArrayAdapter<Gift> {
  private final Context context;
  private List<Gift> giftz2;

  public MySimpleArrayAdapter(Context context, List<Gift> giftx) {
        super(context, R.layout.listview_rowlayout, giftx);
    this.context = context;
        this.giftz2 = giftx;
      }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.listview_rowlayout, parent, false);

    ImageView iconz = (ImageView) rowView.findViewById(R.id.icon);

    Gift rec_gift = (Gift) getItem(position);

    String icon_string = "R.drawable." + rec_gift.photo_key;

    iconz.setImageResource(getImageId(icon_string));

    return rowView;
  }

  public int getImageId(String imageName) {
        return context.getResources().getIdentifier("drawable/" + imageName, null, context.getPackageName());
    }
} 

In the above code, I want to set the imageView based on the icon_string variable so each row in the listView will get the correct image but it's not making a change at all. 在上面的代码中,我想基于icon_string变量设置imageView,因此listView中的每一行都将获得正确的图像,但它根本没有进行任何更改。

Implement getItem method of your custom adapter: 实现自定义适配器的getItem方法:

@Override
public Gift getItem(int position) {
    if(this.giftz2 != null) {
        this.giftz2.get(position);
    } else {
        return null;
    }
}

And change this: 并改变这个:

public int getImageId(String imageName) {
    return context.getResources().getIdentifier("drawable/" + imageName, null, context.getPackageName());
}

To this: 对此:

public int getImageId(String imageName) {
    return context.getResources().getIdentifier(imageName, "drawable", context.getPackageName());
}

And call method: 和通话方法:

String icon_string = String.valueOf(rec_gift.photo_key);
iconz.setImageResource(getImageId(icon_string));

BTW: Try to reuse your views at your adapters getView method. 顺便说一句:尝试在适配器getView方法中重用您的视图。 If you continue using your implementation, you are going to get memory errors. 如果继续使用实现,则会出现内存错误。

if you are trying to download image from web service and setting that image in imageview then you may use image loader 如果您尝试从Web服务下载图像并在imageview中设置该图像,则可以使用图像加载器

put this line in your adapter where you want to display image and pass the parameters 将此行放在要显示图像的适配器中并传递参数

ImageLoader_class(image_url, image_view , progressbar);

// new creat a class with the name of ImageLoader_class and paste this code in that // new创建一个名为ImageLoader_class的类,并将此代码粘贴到其中

    import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Stack;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;




public class ImageLoader_class{
    private static MemoryCache memoryCache = new MemoryCache();
    // private static FileCache fileCache;
    private static boolean isImageLoaderClass = true;
    private static ImageLoader_circle_img imageLoader = null;
    final static int stub_id = R.drawable.ic_launcher;
    private static Bitmap defaultBitmap = null;
    private static Context mContext;

    /**
     * This is constructor of ImageLoader Class make the background thread low
     * priority. This way it will not affect the UI performance *
     * 
     * @param mContext
     *            application context
     */
    private ImageLoader_circle_img(Context context) {
        mContext = context;

        photoLoaderThread.setPriority(Thread.NORM_PRIORITY - 1);

        // fileCache = new FileCache(mContext);
    }

    /**
     * This Method is used to implement singleton concept in this class
     * 
     * @param context
     *            application context
     * @return Class object of this class
     */
    public static ImageLoader_circle_img getInstanceImageLoader(Context context) {
        if (isImageLoaderClass) {
            isImageLoaderClass = false;
            imageLoader = new ImageLoader_circle_img(context);
            return imageLoader;
        }
        return imageLoader;
    }

    /**
     * This method is used to set image in image view
     * 
     * @param url
     *            from this URL, image will be down-load
     * @param imageView
     *            in this view set the image bitmap
     * @param progressBar
     *            which is show when image is displaying
     */

    @SuppressWarnings("deprecation")
    public static void DisplayImage(String url, ImageView imageView,
            ProgressBar progressBar) {

        if (url == null || url.equals("")) {
            imageView.setVisibility(View.VISIBLE);
            // imageView.setImageBitmap(getDefaultBitmap());
            imageView.setBackgroundDrawable(new BitmapDrawable(
                    getDefaultBitmap()));
            progressBar.setVisibility(View.INVISIBLE);


        } else if (memoryCache.get(url) != null) {
            imageView.setVisibility(View.VISIBLE);
            // imageView.setImageBitmap(memoryCache.get(url));
            imageView.setBackgroundDrawable(new BitmapDrawable(memoryCache
                    .get(url)));
            progressBar.setVisibility(View.INVISIBLE);

        } else {
            queuePhoto(url, imageView, progressBar);
            System.out.println("url"+url);
        }
    }
    public static Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
        // TODO Auto-generated method stub
        int targetWidth = 100;
        int targetHeight = 100;
        Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
                Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(targetBitmap);
        Path path = new Path();
        path.addCircle(((float) targetWidth - 1) / 2,
                ((float) targetHeight - 1) / 2,
                (Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
                Path.Direction.CCW);

        canvas.clipPath(path);
        Bitmap sourceBitmap = scaleBitmapImage;
        canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
                sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
                targetHeight), null);
        return targetBitmap;
    }
    /**
     * this method is used to get default bitmap if image url is null
     * 
     * @return bitmap
     */

    private static Bitmap getDefaultBitmap() {
        try {
            defaultBitmap = BitmapFactory.decodeResource(mContext
                    .getResources(), stub_id);
            int width = defaultBitmap.getWidth();
            int height = defaultBitmap.getHeight();
            return Bitmap.createScaledBitmap(defaultBitmap, width / 4,
                    height / 4, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * If bitmap is not downloaded then add in queue
     * 
     * @param url
     *            from this URL, image will be down-load
     * @param imageView
     *            in this view set the image bitmap
     * @param progressBar
     *            which is show when image is displaying
     */
    private static void queuePhoto(String url, ImageView imageView,
            ProgressBar progressBar) {
        photosQueue.Clean(imageView);
        PhotoToLoad p = new PhotoToLoad(url, imageView, progressBar);
        synchronized (photosQueue.photosToLoad) {
            photosQueue.photosToLoad.push(p);
            photosQueue.photosToLoad.notifyAll();
        }

        // start thread if it's not started yet
        if (photoLoaderThread.getState() == Thread.State.NEW)
            photoLoaderThread.start();
    }

    /**
     * this method is used to download bitmap
     * 
     * @param url
     *            from this URL, image will be down-load
     * @return bitmap
     */
    private static Bitmap getBitmap(String url) {

        URL myFileUrl = null;
        Bitmap bmImg = null;
        try {
            myFileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bmImg = BitmapFactory.decodeStream(is);

        } catch (IOException e) {
            e.printStackTrace();
        }
        if (bmImg != null)


        System.out.println(" bmImg.getWidth()"+ bmImg.getWidth());

        if(bmImg.getWidth()>300)
        {
            bmImg = Bitmap.createScaledBitmap(bmImg, bmImg.getWidth(),
                    bmImg.getHeight() , true);

        //  bmImg = Bitmap.createScaledBitmap(bmImg, bmImg.getWidth() / 4,
            //      bmImg.getHeight() / 4, true);
            System.out.println("IF STAT");
        }
        else
        {
            bmImg = Bitmap.createScaledBitmap(bmImg, 55,
                    55, true);

        }
        //Bitmap bt = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        //imageView.setImageBitmap(getRoundedShape(bmImg));
        //if the image needed in round shape
        //return getRoundedShape(bmImg);
        return bmImg;
    }

    /**
     * This class is used to manage url, imageview and progressBar
     */
    private static class PhotoToLoad {
        public String url;
        public ImageView imageView;
        public ProgressBar view;

        public PhotoToLoad(String u, ImageView i, ProgressBar v) {
            url = u;
            imageView = i;
            view = v;
        }
    }

    private static PhotosQueue photosQueue = new PhotosQueue();

    private static void stopThread() {
        photoLoaderThread.interrupt();
    }

    /**
     * this class is used to store list of photoes
     */
    private static class PhotosQueue {
        private Stack<PhotoToLoad> photosToLoad = new Stack<PhotoToLoad>();


        // removes all instances of this ImageView
        public void Clean(ImageView image) {

            for (int j = 0; j < photosToLoad.size();) {

                if (photosToLoad.get(j).imageView == image)

                    photosToLoad.remove(j);
                else
                    ++j;
            }
        }
    }

    /**
     * This thread is used to set photoes in imageView
     */
    private static class PhotosLoader extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    // thread waits until there are any images to load in the
                    // queue
                    if (photosQueue.photosToLoad.size() == 0) {
                        synchronized (photosQueue.photosToLoad) {
                            photosQueue.photosToLoad.wait();
                        }
                    }
                    if (photosQueue.photosToLoad.size() != 0) {
                        PhotoToLoad photoToLoad;
                        synchronized (photosQueue.photosToLoad) {
                            photoToLoad = photosQueue.photosToLoad.pop();
                        }
                        Bitmap bmp;
                        try {
                            bmp = getBitmap(photoToLoad.url);
                            memoryCache.put(photoToLoad.url, bmp);
                            if (((String) photoToLoad.imageView.getTag()).equals(photoToLoad.url)) {
                                BitmapDisplayer bd = new BitmapDisplayer(bmp,
                                        photoToLoad.imageView, photoToLoad.view);
                                Activity a = (Activity) photoToLoad.imageView
                                        .getContext();
                                a.runOnUiThread(bd);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (Thread.interrupted())
                        break;
                }
            } catch (InterruptedException e) {
                // allow thread to exit
            }
        }
    }

    private static PhotosLoader photoLoaderThread = new PhotosLoader();

    /**
     * Used to display bitmap in the UI thread
     */
    private static class BitmapDisplayer implements Runnable {
        Bitmap bitmap;
        ImageView imageView;
        ProgressBar view;

        public BitmapDisplayer(Bitmap b, ImageView i, ProgressBar v) {
            bitmap = b;
            imageView = i;
            view = v;
        }

        @SuppressWarnings("deprecation")
        public void run() {
            if (bitmap != null) {
                imageView.setVisibility(View.VISIBLE);
                // imageView.setImageBitmap(bitmap);
                imageView.setBackgroundDrawable(new BitmapDrawable(bitmap));

                view.setVisibility(View.INVISIBLE);
            }
        }
    }

    /**
     * this function is used to clear all stored data
     */
    public static void clearCache() {
        stopThread();

        // clear memory cache
        memoryCache.clear();

        // clear SD cache
        /*
         * if (fileCache != null) { fileCache.clear(); }
         */
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用自定义适配器为列表视图内的项目设置ItemClickListener? - How to set an ItemClickListener for an item inside the list view with custom adapter? Android:如何在列表中动态设置ImageView src - Android: How to set the ImageView src in a list dynamically 如何设定清单 <String> 进入Android上用于ImageView的RecyclerView适配器 - How to set List<String> into RecyclerView adapter for ImageView on Android 使用Android应用程序的自定义适配器动态添加项目到列表视图 - Dynamically add items to list view using custom adapter for Android app 使用自定义适配器将项目动态添加到列表视图中的问题 - issue to add items dynamically into list view with custom adapter 如何从 ImageView 列表中动态获取 imageview 并将 imageview 放置到 Z6C666B7F9E6BE3A848F2F 变量中 - How to dynamically take a imageview from an ImageView List and place that imageview to an imageview variable Android:具有基本适配器的自定义列表视图:如何在点击列表器中设置行项目? - Android: custom List view with base adapter: How to set on click listner for row items? 如何动态添加视图到自定义光标适配器..? - how to dynamically add view to a custom cursor adapter..? 将列表视图的数据从Asynctask传递到自定义列表适配器类 - Passing data for list view from Asynctask to a custom list adapter class 从RecyclerView适配器设置ImageView滤色器 - Set ImageView color filter from adapter of RecyclerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM