0

I have a ListView that should display items that contain some text and an image.

The data is loaded from a local SQLite db file (that contains the text and image URLs).

I'd like to:

  • Get the text, url from DB.
  • Asynchronously download the image from the URL
  • Bind both values to the ListView (using the SimpleCursorAdapter).

So far, i was able to read the values from DB, however i am not sure how i can run the bind only AFTER i have successfully loaded each image?

In other words, i'd like to asynchronously bind each element as it's loaded to the appropriate UI item.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
lysergic-acid
  • 19,570
  • 21
  • 109
  • 218

2 Answers2

1

Here is a nice example which shows how to this http://www.androidkit.com/loading-images-from-remote-server-over-http-on-a-separate-thread.

Briefly,

1) you need to have a Map<Url, Bitmap>.
2) Have a default image that is displayed when image data from server is not available yet.
3) Have onScroll listener for your ListView, to know which items are currently displayed.
4) First, download those that are being displayed.
5) Once an image is downloaded, call notifyDataSetChanged() to bind available Image to the view.
6) You can use Softreferences or LRUCache to avoid OutofMemoryException

sujith
  • 2,421
  • 2
  • 17
  • 26
  • @sajith the This web page is not available – dvrm Jul 12 '15 at 07:44
  • @dvrm looks like they removed the page. You can look at similar questions in StackOverFlow. ex http://stackoverflow.com/questions/5458395/loading-images-from-remote-server – sujith Jul 14 '15 at 07:39
-1

I have solved a similar problem to this. I received a XML from server and store the information in a Database. After that I populated the list using the CursorAdapter. In my case I have both images and text.

To solve the problem in the cursor adapter I did something like this:

@Override
public void bindView(View v, Context ctx, Cursor c) {
    TextView title =  (TextView) v.findViewById(R.id.titleID);
    title.setText(c.getString(c.getColumnIndex(yourColumName)));
    ImageView i =  (ImageView) v.findViewById(R.id.ImageID); 
    String s = c.getString(c.getColumnIndex(youtImageColumn));
    imageLoader.DisplayImage(s,i);
}


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View v = inflater.inflate(R.layout.yourRowLayout, parent, false);
    return v;
}

In this case the ImageLoader is an async lazy image loader gotten from here: https://github.com/thest1/LazyList

rdiaz82
  • 996
  • 12
  • 31