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.
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;
}
}