【发布时间】:2016-05-02 09:23:50
【问题描述】:
我正在尝试根据调用方法的某些条件来传递类。如果在 Albums 中调用方法,则使用类“album”,但如果在 Artists 中调用,则使用类“artist”。
我还是 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;
}
下面是一个类“Album”,是需要调用的类的一个例子。
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;
}
也许我只是需要一种新的架构来解决这个问题?
【问题讨论】:
-
你使用的是哪个版本的java?
-
@kakurala 版本 7。
-
IS_MUSIC 中有什么?还有,你为什么要通过这门课?我想这取决于你所处的观点,对吧?就像如果您在艺术家的视图中,它将显示所有艺术家的歌曲,如果您在特定专辑的视图中,它将显示专辑的所有歌曲?
-
IS_MUSIC 是我用来检查返回的行是否是音乐而不是铃声或其他内容的列。
标签: java class methods reflection mediastore