对 ArrayList 中的相同项目进行分组 - MusicPlayer

Grouping Identical Items in an ArrayList - MusicPlayer

我正在尝试将几个 ArrayList 组合成一个音乐播放器。当前列表为每首歌曲生成艺术家,而不仅仅是一位艺术家。希望这能更好地解释它:

我的一个片段的当前代码是这样的:

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;
}
}

我看过 Map,我看过 Set,现在我很困惑...

那么,我如何将我的 ArrayList 分组以删除不同艺术家的重复项,然后最终使用 OnPickedArrayList 更改为 [= 中的那些分组歌曲33=]?

我是否在正确的行上,或者是否有完全不同的排序方法 Genres/Artists/Albums 等等?

如果清除重复项是您唯一的问题,那么这是清除列表中所有重复项的方法(将其转换为集合并返回):

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

或单行

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

您还应该根据使列表条目唯一的字段覆盖 equals() 和 hashCode() 方法。