0

I am having very difficult time understanding all the processes (in google and SO) of loading image in list view. However, I manage to get the system working following example here.

But the images loaded are per view and when I change the view (scroll), it loads again [I guess it is the system]. And more important issue is, you can see image below, I have partial view of book number 11 at the bottom, hence the image is loaded for top book (book#1). If I scroll down and then go top again, then Book#1 loads that default grayish image, as expected ... so this problem happens only during first time load

Is it possible to load the images AT ONCE for all the lists in the background task so that everytime I scroll, it does not load new images? How can I tackle the problem mentioned in the image below?

I have made some modification with kind help of one SO member to include a checkbox though. Below is the changed part from the example. Rest classes (ImageLoader, MemoryCache, Utils, FileCache) are exactly copy from the example above.

enter image description here

Base Adapter Class [changed from the example]

public class MyList extends BaseAdapter {

    static HashSet<String> selectedBooks = new HashSet<String>();
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public MyList(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    CompoundButton.OnCheckedChangeListener checkChangedListener = new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            String bookId = (String) buttonView.getTag();
            if(isChecked){
                selectedBooks.add(bookId);
            }else{
                selectedBooks.remove(bookId);
            }
        }
    };

    public static String[] getSelectedBooks(){
        return selectedBooks.toArray(new String [selectedBooks.size()]);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        //return position;
        return data.get(position);
    }

    public long getItemId(int position) {
        //return position;
        return Long.parseLong(data.get(position).get(ShowList.LIST_KEY_ID));
    }

    static class ViewHolder {
        TextView title;
        TextView writer;
        TextView bookid;
        CheckBox check;
        ImageView thumb;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            convertView = inflater.inflate(R.layout.listrow_row, null);
            holder = new ViewHolder();
            holder.title = (TextView)convertView.findViewById(R.id.title);
            holder.writer = (TextView)convertView.findViewById(R.id.writer);
            holder.bookid = (TextView)convertView.findViewById(R.id.bookid);
            holder.thumb = (ImageView)convertView.findViewById(R.id.thumb);
            holder.check = (CheckBox)convertView.findViewById(R.id.check);
            holder.check.setOnCheckedChangeListener(checkChangedListener);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        @SuppressWarnings("unchecked")
        HashMap<String, String> book = (HashMap<String, String>) getItem(position);
        holder.check.setTag(book.get(ShowList.LIST_KEY_ID));
        holder.title.setText(book.get(ShowList.LIST_KEY_NAME));
        holder.writer.setText(book.get(ShowList.LIST_KEY_WRITER));
        //holder.thumb.setImageResource(R.drawable.no_cover);
        holder.bookid.setText(book.get(ShowList.LIST_KEY_ID));
        if(!book.get(ShowList.LIST_ISBN).trim().equals("")){
            Log.d("name","NAME: "+book.get(ShowList.LIST_KEY_NAME));
            Log.d("isbn","ISBN: "+book.get(ShowList.LIST_ISBN));
            imageLoader.DisplayImage(book.get(ShowList.LIST_ISBN), holder.thumb);
        }
        boolean bookSelected = false;
        if(selectedBooks.contains(book.get(ShowList.LIST_KEY_ID))){
            bookSelected = true;
        }
        holder.check.setChecked(bookSelected);
        return convertView;
    }
}
abdfahim
  • 2,523
  • 10
  • 35
  • 71
  • [This](http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview) will Help you and all to explore More. – Rupesh Yadav Dec 11 '13 at 06:39

3 Answers3

0

Use lazy load list.

ImageLoader imageLoader =new ImageLoader(this);
imageLoader.DisplayImage(imageURL, ImageView);

URL :https://github.com/thest1/LazyList

Ashwin S Ashok
  • 3,623
  • 2
  • 29
  • 36
0

Write else part for the image that you set:

 if(!book.get(ShowList.LIST_ISBN).trim().equals("")){
            Log.d("name","NAME: "+book.get(ShowList.LIST_KEY_NAME));
            Log.d("isbn","ISBN: "+book.get(ShowList.LIST_ISBN));
            imageLoader.DisplayImage(book.get(ShowList.LIST_ISBN), holder.thumb);
        }else{
        holder.thumb.setImageResource(<DefaultImage>);


}
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • do you have some image that u show in case of book-1?? or it's default one – vipul mittal Dec 11 '13 at 05:43
  • nope .. for Book#1, LIST_ISBN is "" (empty), and I have already default_image loaded in xml for each row. Also, interesting thing is, if I scroll down once and then again go top, then Book#1 has that default grayish image, as expected ... so this problem happens only during first time load – abdfahim Dec 11 '13 at 05:46
0

solved the issue just by changing layout_weight & layout_height= "fill_parent" ...

abdfahim
  • 2,523
  • 10
  • 35
  • 71