我正在尝试根据调用方法的某些条件传递类。如果在专辑中调用方法,请使用“专辑”类,但如果在艺术家中调用,请使用“艺术家”类。
我还是Java新手,似乎无法掌握这一点。
以下是调用这些不同类的方法。
private int songCounter;
public ArrayList getSongs(Context context, String type, int max, Class cls){
String select;
switch (type) {
case "albums": {
select = IS_MUSIC + " != 0 AND ALBUM NOT NULL) GROUP BY (ALBUM";
break;
}
case "artists": {
select = IS_MUSIC + " != 0 AND ARTIST NOT NULL) GROUP BY (ARTIST";
break;
}
default: {
select = IS_MUSIC + " != 0";
break;
}
}
ArrayList songList = new ArrayList();
Cursor c = context.getContentResolver().query(EXTERNAL_CONTENT_URI,null,select,null,null);
if(c!= null && c.moveToFirst()){
do {
long id = c.getLong(c.getColumnIndex(_ID));
long albumId = c.getLong(c.getColumnIndex(ALBUM_ID));
String artist = c.getString(c.getColumnIndex(ARTIST));
String album = c.getString(c.getColumnIndex(ALBUM));
String title = c.getString(c.getColumnIndex(TITLE));
songCounter++;
if (songCounter < max) {
/////// I'm having a problem here, how do I call "cls"?
songList.add(new cls(id, album, artist, title, getArt(context, albumId)));
}
}
while (c.moveToNext());
}
if (c != null) {
c.close();
}
return songList;
}
下面是一个“专辑”类,这是需要调用的类的一个示例。
public class Album {
private long id;
private String title;
private String artist;
private String album;
private String year;
private String genre;
private String duration;
private BitmapDrawable art;
public Album(long songID, String songTitle, String songArtist, String songAlbum, BitmapDrawable songArt) {
id = songID;
title = songTitle;
artist = songArtist;
album = songAlbum;
art = songArt;
}
public long getID(){
return id;
}
public String getTitle(){
return title;
}
public String getArtist(){
return artist;
}
public String getAlbum(){
return album;
}
public String getDuration(){
return duration;
}
public BitmapDrawable getArt(){
return art;
}
}
以及我如何调用“getSongs”方法。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreateView(inflater,container,savedInstanceState);
View rootView = inflater.inflate(R.layout.library_albums, container, false);
this.mView = rootView;
songView = (ListView) mView.findViewById(R.id.album_list);
songList = new ArrayList<Album>();
this.music = new Music();
This is where I don't know what to do also/////
songList = music.getSongs(mView.getContext(), "albums", 200, "album/artist/song class here);
AlbumAdapter songAdt = new AlbumAdapter(mView.getContext(), songList);
songView.setAdapter(songAdt);
return rootView;
}
也许我只需要一种新的架构?
答案 0 :(得分:1)
你可以做
Album extends SongCollection
和
Artist extends SongCollection
在SongCollection
中,您将拥有
abstract List<Song> getSongs();
在Artist
和Album
中,您将覆盖getSongs()
,返回正确的查询结果。
对于查询,您可能希望使用方法MusicDatabase
List<Song> query(String queryText, String... params)
答案 1 :(得分:0)
忽略代码中存在的许多替代设计,并仅关注问题标题,您可以执行以下操作: (这是我的头,未经测试的代码直接写入浏览器,因此可能需要调整编译)
public <T extends BaseSongs> ArrayList<T> getSongs(Context context, int max, Class<T> cls){
String select;
switch (cls. getName()) {
case "Albums": {
select = IS_MUSIC + " != 0 AND ALBUM NOT NULL) GROUP BY (ALBUM";
break;
}
case "Artists": {
select = IS_MUSIC + " != 0 AND ARTIST NOT NULL) GROUP BY (ARTIST";
break;
}
default: {
select = IS_MUSIC + " != 0";
break;
}
}
ArrayList<BaseSongs> songList = new ArrayList<>(); // java 7
if (songCounter < max) {
/////// Use Reflection
Constructor ctr = cls.getConstructor(<list the types for ctr args here>);
songList.add(ctr.newInstance(id, album, artist, title, getArt(context, albumId)));
}
return songList;
你调用你的getSongs()的方式是:
songList = music.getSongs(mView.getContext(), 200, Albums.class);