Search code examples
androidandroid-cursoradapter

Getting an image from a URL


I pull articles from a server in the form of JSON objects, where an image related to the article is an attribute of the objects in the form of a URL. The URL is then saved locally, along with other data, in a database in the device.

Now, in order to send the data to the UI, I want to use a CursorAdapter. My question is this:

Is it considered bad practice if I set an ImageView from a URL in the inherited bindView method and how would I do that?

edit: For the sake of clarity, I should mention that the JSON objects are parsed, and the relevant data is pulled into strings that are stored in a local database.


Solution

  • Is it considered bad practice if I set an ImageView from a URL in the inherited bindView method

    Directly? Yes, as that would cause the network I/O to be done on the main application thread, which freezes your UI while that I/O is going on. Instead, use any one of a number of image loading libraries that handle this for you asynchronously, like Picasso.

    (BTW, I am not quite sure why/how you are shoving JSON data into something that implements Cursor -- you may be better off just putting POJOs in an array and using ArrayAdapter, or creating a custom subclass of BaseAdapter that uses your parsed JSON more directly).