Android: Help in adapting ListView adapter with an ImageLoader Class (LazyList)

后端 未结 4 705
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 14:40

I have a custom ListView adapter which implements an ImageThreadLoader class. Unfortunately the class doesn\'t enable a cache option-download the images from the web and sav

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-15 14:48

    i do have the sam eproblem of different datatype other than String array but I am using Arraylist(but not JASON) so I do changes in the code of Fedor and below is my implementation, hope this help

    LazyAdapter

    public LazyAdapter(Activity a, ArrayList mStatus)
    {
        activity = a;
        data = mStatus;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }
    
    public int getCount()
    {
        if(data != null  && data.size() > 15)
            return data.size();
        else
            return 15;
    }
    

    I found that even if the files in sdcard it doesnt load becuase its checking for that object which goes null if you change the activity on which list is implemented so I changed getBitmap() code also ImageLoader

    private Bitmap getBitmap(String urlString)
    {
        String filename = String.valueOf(urlString.substring(urlString.lastIndexOf("/") + 1));
        File f = new File(cacheDir, filename);
        try
        {
            if(!f.exists())
            {
                Bitmap bitmap = null;
                InputStream is = new URL(urlString).openStream();
                OutputStream os = new FileOutputStream(f);
                Globals.CopyStream(is, os);
                os.close();
                bitmap = decodeFile(f);
                return bitmap;
            }
            else
            {               
                Bitmap bitmap = decodeFile(f);
                return bitmap;
            }
    
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            BitmapDrawable mDrawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.placeholder);
            return mDrawable.getBitmap();
        }
    }
    

提交回复
热议问题