0

When the user clicks on a button, a new ListFragment is displayed. This fragment will contain the list of the albums with their associated Artist's name.

I've created my own class AlbumItem (String name, String artist) with name being the Album's title and artist the corresponding artist name :

public class AlbumItem {

    private String AlbumName;
    private String AlbumArtist;

    public AlbumItem(){
    }

    public AlbumItem(String name, String artist){
        this.AlbumName = name;
        this.AlbumArtist = artist;
    }

    public String getAlbumName() {
        return AlbumName;
        }

    public void setAlbumName(String AlbumName) {
        this.AlbumName = AlbumName;
        }

    public String getAlbumArtist() {
        return AlbumArtist;
        }

    public void setAlbumArtist(String AlbumArtist) {
        this.AlbumArtist = AlbumArtist;
        }
}

Then I wrote my custom adapter which associates the Album's name and Artist's name with the correct TextView in my ListView's row.

So then I can declare an ArrayList of AlbumItem and fill it like this :

ArrayList<AlbumItem> arrayList;
arrayList.add(new AlbumItem ("Album's title", "Artist");

Now I have few questions :

1) Am I going the appropriate way ? I've always learnt to fill listviews like that and I'm very comfortable using this technique (Custom Item class + ArrayList + CustomAdapter), but I'm doing a Music player and I'd like to query the list of Albums and update the listview asynchronously so that the UI is not blocked. I don't know if it is possible to do it by loading data in a ArrayList the way I do it.

2) How to fill up this ArrayList asynchronously ? With LoaderManager/CursorLoader or Asyntask or something else ? (I'm targeting Android 4.0)

Thanks for your advice.

Jecimi
  • 4,113
  • 7
  • 31
  • 40
  • Where are you filling the list from? DB? Web? If it's from the db I don't think you actually need it to be asyncronously (unless it's a very long operation and it takes long and the user notices it. Otherwise, I'd sugest AsyncTask: http://developer.android.com/reference/android/os/AsyncTask.html – Nuno Gonçalves Jun 13 '12 at 21:37
  • Since you target the latest APIs, I'd recommend to use ListFragment + Loader (CursorLoader if your data is stored in DB). – Vit Khudenko Jun 13 '12 at 21:37
  • @ Nuno Gonçalves : Well if I query only Artists or Albums, you're right, it may not be always useful to process asyncronously. But If the list is very long or if I want later to query thumbnails of covers, I think there's a risk that the UI is slowed down. – Jecimi Jun 13 '12 at 22:05

1 Answers1

0

CursorLoader is a convenient way of loading your list, especially if you get the data from a database or another local data source. If your data comes from the network, it may be easier to get the data with an asynctask and load it in the list adapter. If you are familiar with one of these, use that.

Christine
  • 5,617
  • 4
  • 38
  • 61
  • The problem is that there's no example of a CursorLoader with a CustomAdapter and I've been strugguling for hours trying with a CursorLoader :/. If I could only find an example of a CursorLoader filling a simple Array... So I think I'll give a try to AsyncTask first. – Jecimi Jun 13 '12 at 22:01
  • @Jecimi: just a tip. `CursorLoader` implies your data is backed by a `ContentProvider`, so if you don't use `ContentProvider`, then you either need to create your custom `AsyncTaskLoader` to read the data from DB or use `AsyncTask` that reads the data from DB in the `AsyncTask.doInBackground()`. – Vit Khudenko Jun 13 '12 at 22:49
  • @Arhimed: Thanks for the tip. I do use a `ContentProvider` but CursorLoader is so complicated to use that I'm thinking to use `AsyncTask`. I'll try both method and I'll see. If anyone has a link for a simple LoaderManager/CursorLoader example, I'd be very ice. – Jecimi Jun 13 '12 at 23:14
  • Jecimi, if you already have a contentProvider, I second Arhimeds opinion that it's best to use a cursorloader. It's not that complicated, once you see how it works. It's way more elegant than programming loading yourself in an asynctask. – Christine Jun 14 '12 at 05:31
  • Finally solved. I've abandoned my array list and my custom class (snif :'( ) to use a LoaderManager and a CursorLoader with a CustomAdapter. It is indeed more efficient. It is easy **only once you've understood how it works**, which isn't easy as I found no example of LoaderManager/CursorLoader/CustomAdapter together. If you're in the same situation, see my [post](http://stackoverflow.com/questions/11025350/display-album-and-artist-in-listview-with-loadermanager-and-cursorloader/11028923#11028923) for a simple example of all this. – Jecimi Jun 14 '12 at 08:32
  • I'm glad you got it working and that you're happy with it :-) – Christine Jun 15 '12 at 03:48