Search code examples
javaandroidarraylistadapterandroid-music-player

Grouping Identical Items in an ArrayList - MusicPlayer


I'm trying to group of several ArrayLists for a music player. The current list generates the artists for every song instead of just the one artist. Hopefully this should explain it better:

Screenshot

My current code for one of my fragments is this:

public class ArtistsFragment extends Fragment {

private ArrayList<Artist> artistList;
View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_artists, container, false);

    GridView gvArtists = (GridView) view.findViewById(R.id.gvArtists);

    //instantiate list
    artistList = new ArrayList<>();

    //get artists from device
    getArtistList();

    // Group ArrayList..
    artistList = new ArrayList<>(new HashSet<>(artistList));

    //sort alphabetically by title
    Collections.sort(artistList, new Comparator<Artist>() {
                public int compare(Artist a, Artist b) {
                    return a.getArtist().compareTo(b.getArtist());
                }
            }
    );

    //create and set adapter
    ArtistAdapter artistAdt = new ArtistAdapter(getActivity(), artistList);
    gvArtists.setAdapter(artistAdt);

    return view;
}

void getArtistList() {
    //retrieve artist info
    ContentResolver musicResolver = getActivity().getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

    if (musicCursor != null && musicCursor.moveToFirst()) {
        //get columns
        int idColumn = musicCursor.getColumnIndex
                (MediaStore.Audio.Media._ID);
        int artistColumn = musicCursor.getColumnIndex
                (MediaStore.Audio.Albums.ARTIST);
        int artColumn = musicCursor.getColumnIndex
                (MediaStore.Audio.Media.ARTIST_ID);

        //add artists to list
        do {
            long thisId = musicCursor.getLong(idColumn);
            String thisArtist = musicCursor.getString(artistColumn);
            String thisArt = musicCursor.getString(artColumn);

            artistList.add(new Artist(thisId, thisArtist, thisArt));
        }
        while (musicCursor.moveToNext());
        musicCursor.close();
    }
}

}

Artist.class

class Artist {
private final long id;
private final String artist;
private final String art;

public Artist(long artistID, String theartist, String artistArt) {
    id = artistID;
    artist = theartist;
    art = artistArt;
}

public long getID() {
    return id;
}

public String getArtist() {
    return artist;
}

public String getArt() {
    return art;
}
}

I've looked at Map, I've looked at Set and now i'm just confused...

So, how do I group my ArrayList to remove the duplicates of different artists to then eventually use the OnPicked to change the ArrayList to those grouped songs within that group/category?

Am I even on the right lines or is there a completely different method to sorting Genres/Artists/Albums Etc.?


Solution

  • if clearing your duplicates is your sole problem, then this is how to clear all duplicates on your List (convert it to a Set and back):

    Set<Artist> set = new HashSet<Artist>(artistList);
    artistList = new ArrayList<Artist>(set);
    //then sort it...
    

    or one-line

    artistList = new ArrayList<Artist>(new HashSet<Artist>(artistList));
    

    You should also override the equals() and hashCode() methods based on the fields that make an entry to your list unique.