lundi 2 mai 2016

How to pass a class as a parameter to a method as if it were a dynamic variable?

I'm trying to pass classes depending on some conditions of where a method is being called. If a method is called in Albums, use class "album", but if is called in Artists use class "artist".

I'm still new in Java, can't seem to get hold of this.

Here's the method that's suppose to call these different classes.

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

Below is a class "Album", an example of the kind of classes that needs to be called.

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

}

And how I'm trying to call the "getSongs" method.

@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;
    }

Maybe I just need a new kind of architecture for this?





Aucun commentaire:

Enregistrer un commentaire