سؤال

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.

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top