在ArrayList中分组相同的项目 - 音乐播放器

时间:2015-01-06 21:41:30

标签: java android arraylist adapter android-music-player

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

Screenshot

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

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更改为该组/类别中的分组歌曲?

我是否在正确的界限上或者是否有完全不同的方法来排序流派/艺术家/专辑等?

1 个答案:

答案 0 :(得分:1)

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

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()方法。