【问题标题】:Combining an imageview with some text in listview continued将图像视图与列表视图中的一些文本结合起来(续)
【发布时间】:2014-07-02 20:49:04
【问题描述】:

作为对我的previous question 的回应,我确实创建了所有建议的类并添加了picasso 库。

我还添加了list_item 布局文件,它应该显示带有一些文本的图像。然后在主要活动中,我试图让它发挥作用。我收到了来自ListView.setAdapter(adapter); 的错误说Cannot make a static reference to the non-static method setAdapter(ListAdapter) from the type ListView

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ExampleViewModel firstRow = new ExampleViewModel("First Row", "http://upload.wikimedia.org/wikipedia/commons/6/6f/Freiburger_Alpen.JPG");    
        ExampleViewModel secondRow = new ExampleViewModel("Second Row", "http://blog.caranddriver.com/wp-content/uploads/2013/05/lamborghini_egoista_three_quarter_front_view.jpg");    
        ExampleViewModel thirdRow = new ExampleViewModel("Third Row", "http://4.bp.blogspot.com/-vXnf7GjcXmg/UfJZE9rWc2I/AAAAAAAAGRc/x2CIlHM9IAA/s1600/aphoto49721.jpg");

        List<ExampleViewModel> viewModels = new ArrayList<ExampleViewModel>();
        viewModels.add(firstRow);
        viewModels.add(secondRow);
        viewModels.add(thirdRow);

        ExampleAdapter adapter = new ExampleAdapter(this, viewModels);
        ListView.setAdapter(adapter);

    }
}

【问题讨论】:

    标签: android listview


    【解决方案1】:

    在您的适配器类中声明 ImageLoader 类的对象

    试试这个代码:

    imageLoader.DisplayImage(ImageUrl, ItemImageView);
    

    这里是 ImageLoader

    public class ImageLoader {
    
        MemoryCache memoryCache=new MemoryCache();
        FileCache fileCache;
        private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
        ExecutorService executorService;
        Handler handler=new Handler();//handler to display images in UI thread
    
        public ImageLoader(Context context){
            fileCache=new FileCache(context);
            executorService=Executors.newFixedThreadPool(5);
        }
    
        final int stub_id=R.drawable.no_image;
        public void DisplayImage(String url, ImageView imageView)
        {
            imageViews.put(imageView, url);
            Bitmap bitmap=memoryCache.get(url);
            if(bitmap!=null)
                imageView.setImageBitmap(bitmap);
            else
            {
                queuePhoto(url, imageView);
                imageView.setImageResource(stub_id);
            }
        }
    
        private void queuePhoto(String url, ImageView imageView)
        {
            PhotoToLoad p=new PhotoToLoad(url, imageView);
            executorService.submit(new PhotosLoader(p));
        }
    
        private Bitmap getBitmap(String url) 
        {
            File f=fileCache.getFile(url);
    
            //from SD cache
            Bitmap b = decodeFile(f);
            if(b!=null)
                return b;
    
            //from web
            try {
                Bitmap bitmap=null;
                URL imageUrl = new URL(url);
                HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
                conn.setConnectTimeout(30000);
                conn.setReadTimeout(30000);
                conn.setInstanceFollowRedirects(true);
                InputStream is=conn.getInputStream();
                OutputStream os = new FileOutputStream(f);
                Utils.CopyStream(is, os);
                os.close();
                conn.disconnect();
                bitmap = decodeFile(f);
                return bitmap;
            } catch (Throwable ex){
               ex.printStackTrace();
               if(ex instanceof OutOfMemoryError)
                   memoryCache.clear();
               return null;
            }
        }
    
        //decodes image and scales it to reduce memory consumption
        private Bitmap decodeFile(File f){
            try {
                //decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                FileInputStream stream1=new FileInputStream(f);
                BitmapFactory.decodeStream(stream1,null,o);
                stream1.close();
    
                //Find the correct scale value. It should be the power of 2.
                final int REQUIRED_SIZE=70;
                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;
                FileInputStream stream2=new FileInputStream(f);
                Bitmap bitmap=BitmapFactory.decodeStream(stream2, null, o2);
                stream2.close();
                return bitmap;
            } catch (FileNotFoundException e) {
            } 
            catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        //Task for the queue
        private class PhotoToLoad
        {
            public String url;
            public ImageView imageView;
            public PhotoToLoad(String u, ImageView i){
                url=u; 
                imageView=i;
            }
        }
    
        class PhotosLoader implements Runnable {
            PhotoToLoad photoToLoad;
            PhotosLoader(PhotoToLoad photoToLoad){
                this.photoToLoad=photoToLoad;
            }
    
            public void run() {
                try{
                    if(imageViewReused(photoToLoad))
                        return;
                    Bitmap bmp=getBitmap(photoToLoad.url);
                    memoryCache.put(photoToLoad.url, bmp);
                    if(imageViewReused(photoToLoad))
                        return;
                    BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
                    handler.post(bd);
                }catch(Throwable th){
                    th.printStackTrace();
                }
            }
        }
    
        boolean imageViewReused(PhotoToLoad photoToLoad){
            String tag=imageViews.get(photoToLoad.imageView);
            if(tag==null || !tag.equals(photoToLoad.url))
                return true;
            return false;
        }
    
        //Used to display bitmap in the UI thread
        class BitmapDisplayer implements Runnable
        {
            Bitmap bitmap;
            PhotoToLoad photoToLoad;
            public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
            public void run()
            {
                if(imageViewReused(photoToLoad))
                    return;
                if(bitmap!=null)
                    photoToLoad.imageView.setImageBitmap(bitmap);
                else
                    photoToLoad.imageView.setImageResource(stub_id);
            }
        }
    
        public void clearCache() {
            memoryCache.clear();
            fileCache.clear();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      • 1970-01-01
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多