从其他班级获取歌曲细节

时间:2016-05-29 12:54:52

标签: java android

我正在尝试从数组列表方法中获取数据。我创建了一个类

public class SongDto {

public long songId;
public String songTitle;
public String songArtist;
public String path;
public short genre;
public long duration;
public String album;
public Bitmap albumArt;

public String toString() {
return String.format("songId: %d, Title: %s, Artist: %s, Path: %s, Genere: %d, Duration %s",
        songId, songTitle, songArtist, path, genre, duration);
}

并在另一个类名中获取这些值

public class Utils {
private Context _context;

// constructor
public Utils(Context context) {
    this._context = context;
}
public static ArrayList<SongDto> getMusicInfos(Context context) {

    ArrayList<SongDto> musicInfos = new ArrayList<SongDto>();

现在我想在另一个班级中获得歌曲标题

private ArrayList<SongDto> musicInfos;
private Utils utils;
private ArrayList<String> songPaths = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    utils = new Utils(this);
    songPaths = utils. getMusicInfos(songTitle);
}

private void update(){
    ArrayAdapter<String> list=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,musicInfos.add(songTitle) );
}
}

我如何才能获得所需的数组,如songId,songArtist,歌曲持续时间。我在哪里做错了。如何设置getMusicInfos方法

1 个答案:

答案 0 :(得分:0)

例如,我想使用apache collection utils从SongDto数组中获取标题数组:

Collection<String> songTitles = CollectionUtils.collect(songDtos, new Transformer()  
  {  
    public Object transform(Object o)  
    {  
      String s = ((SongDto) o).title;  
       return s;
    }  
  });  

当然,您可以将集合转换为数组。