【问题标题】:Grouping Identical Items in an ArrayList - MusicPlayer对 ArrayList 中的相同项目进行分组 - MusicPlayer
【发布时间】:2015-01-06 21:41:29
【问题描述】:

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

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

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

}

艺术家.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 更改为该组/类别中的那些分组歌曲?

我是不是在正确的路线上,还是有完全不同的方法来分类流派/艺术家/专辑等?

【问题讨论】:

  • 坦率地说,我没有得到你的要求。
  • 分组是什么意思?在您的示例中,您有一个艺术家列表。你到底想用它做什么?还有其他列表吗?
  • Artist 类的哪些字段使 Artist 独一无二?

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


【解决方案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() 方法。

【讨论】:

  • 谢谢,但这与没有HashSet的结果相同
  • Artist 类中的 equals() 方法是什么?给我们这个类的代码。
  • 您应该添加 equals() 和 hashCode() 方法,这些方法基于使 Artist 满足您的需求的独特字段。
  • 在我的情况下我该怎么做?
  • 艺术家的独特之处是什么?现场艺术家?身份证?它们的组合?可能是艺术家和艺术,对吗?
猜你喜欢
  • 2020-08-14
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 2016-10-28
  • 1970-01-01
  • 2012-11-13
相关资源
最近更新 更多