【发布时间】:2017-05-04 09:17:30
【问题描述】:
我正在构建音乐播放器应用程序,我从 SD 卡收集从服务器下载的数据并将文件名保存到 ArrayList
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
现在的问题是我想根据用户点击回收器项目播放 mp3 文件,为此,我想要特定名称的索引号。
如何获取 index 用于传递 mp3 文件的名称??
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
*/
public ArrayList<HashMap<String, String>> getPlayList() {
File home = new File(MEDIA_PATH);
if (home.listFiles(new FileExtensionFilter()).length > 0) {
for (File file : home.listFiles(new FileExtensionFilter())) {
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
song.put("songPath", file.getPath());
Log.d(TAG, "getPlayList() called title = "+file.getName().substring(0, (file.getName().length() - 4))+" Path = "+file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list array
return songsList;
}
【问题讨论】:
-
我建议你创建一个
Song类而不是将属性存储在 HashMap 中 -
是的,POJO 将是一个不错的选择,但我已经实现了 HashMap,所以现在需要一段时间来实现和更改所有类。