Share filter code and use SparseArray instead of HashMap
This commit is contained in:
parent
3410917239
commit
b767e4eca8
@ -1,8 +1,5 @@
|
||||
package org.kreed.vanilla;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.view.View;
|
||||
@ -11,22 +8,9 @@ import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class AlbumAdapter extends AbstractAdapter {
|
||||
private static Song[] filter(Song[] songs)
|
||||
{
|
||||
HashMap<Integer, Song> albums = new HashMap<Integer, Song>();
|
||||
for (int i = songs.length; --i != -1; ) {
|
||||
Song song = songs[i];
|
||||
if (!albums.containsKey(song.albumId))
|
||||
albums.put(song.albumId, song);
|
||||
}
|
||||
Song[] result = albums.values().toArray(new Song[0]);
|
||||
Arrays.sort(result, new Song.AlbumComparator());
|
||||
return result;
|
||||
}
|
||||
|
||||
public AlbumAdapter(Context context, Song[] allSongs)
|
||||
{
|
||||
super(context, filter(allSongs));
|
||||
super(context, Song.filter(allSongs, new Song.AlbumComparator()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -18,9 +18,6 @@
|
||||
|
||||
package org.kreed.vanilla;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.view.View;
|
||||
@ -28,22 +25,9 @@ import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class ArtistAdapter extends AbstractAdapter {
|
||||
private static Song[] filter(Song[] songs)
|
||||
{
|
||||
HashMap<Integer, Song> artists = new HashMap<Integer, Song>();
|
||||
for (int i = songs.length; --i != -1; ) {
|
||||
Song song = songs[i];
|
||||
if (!artists.containsKey(song.artistId))
|
||||
artists.put(song.artistId, song);
|
||||
}
|
||||
Song[] result = artists.values().toArray(new Song[0]);
|
||||
Arrays.sort(result, new Song.ArtistComparator());
|
||||
return result;
|
||||
}
|
||||
|
||||
public ArtistAdapter(Context context, Song[] allSongs)
|
||||
{
|
||||
super(context, filter(allSongs));
|
||||
super(context, Song.filter(allSongs, new Song.ArtistComparator()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -18,6 +18,7 @@
|
||||
|
||||
package org.kreed.vanilla;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
@ -26,6 +27,7 @@ import android.net.Uri;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.SparseArray;
|
||||
|
||||
public class Song implements Parcelable {
|
||||
int id;
|
||||
@ -196,17 +198,49 @@ public class Song implements Parcelable {
|
||||
}
|
||||
}
|
||||
|
||||
public static class AlbumComparator implements Comparator<Song> {
|
||||
public static class AlbumComparator implements IdComparator {
|
||||
public int compare(Song a, Song b)
|
||||
{
|
||||
return a.album.compareTo(b.album);
|
||||
}
|
||||
|
||||
public int getId(Song song)
|
||||
{
|
||||
return song.albumId;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ArtistComparator implements Comparator<Song> {
|
||||
public static class ArtistComparator implements IdComparator {
|
||||
public int compare(Song a, Song b)
|
||||
{
|
||||
return a.artist.compareTo(b.artist);
|
||||
}
|
||||
|
||||
public int getId(Song song)
|
||||
{
|
||||
return song.artistId;
|
||||
}
|
||||
}
|
||||
|
||||
public static interface IdComparator extends Comparator<Song> {
|
||||
public int getId(Song song);
|
||||
}
|
||||
|
||||
public static Song[] filter(Song[] songs, IdComparator comparator)
|
||||
{
|
||||
SparseArray<Song> albums = new SparseArray<Song>(songs.length);
|
||||
for (int i = songs.length; --i != -1; ) {
|
||||
Song song = songs[i];
|
||||
int id = comparator.getId(song);
|
||||
if (albums.get(id) == null)
|
||||
albums.put(id, song);
|
||||
}
|
||||
|
||||
Song[] result = new Song[albums.size()];
|
||||
for (int i = result.length; --i != -1; )
|
||||
result[i] = albums.valueAt(i);
|
||||
|
||||
Arrays.sort(result, comparator);
|
||||
return result;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user