我正在尝试创建一个音乐播放器(学习)。我从SD卡中检索了歌曲,其中包括歌曲的名称及其路径,该歌曲存储在ArrayList<的HashMap<字符串,字符串>>。
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
ArrayList<HashMap<String, String>> song = new SongsManager().getPlayList();
ArrayAdapter<HashMap<String, String>> songAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, song);
listView.setAdapter(songAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int itemPos = position;
//String itemVal = (String)listView.getItemAtPosition(position);
HashMap<String, String> m = (HashMap<String, String>) listView.getItemAtPosition(position);
String strArr[] = new String[m.size()];
int i = 0;
for (String current : m.values()) {
strArr[i] = current;
i++;
}
Toast.makeText(getApplicationContext(), itemPos+" - "+strArr[1], Toast.LENGTH_SHORT).show();
}
});
}
}
SongsManager.java文件
package com.example.app.musicplayer;
import android.os.Environment;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
public class SongsManager {
final String MEDIA_PATH = Environment.getExternalStorageDirectory()
.getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";
// Constructor
public SongsManager() {
}
/**
* Function to read all mp3 files and store the details in
* ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList() {
System.out.println(MEDIA_PATH);
if (MEDIA_PATH != null) {
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
// return songs list array
return songsList;
}
private void scanDirectory(File directory) {
if (directory != null) {
File[] listFiles = directory.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
}
private void addSongToList(File song) {
if (song.getName().endsWith(mp3Pattern)) {
HashMap<String, String> songMap = new HashMap<String, String>();
songMap.put("songTitle",
song.getName().substring(0, (song.getName().length() - 4)));
songMap.put("songPath", song.getPath());
// Adding each song to SongList
songsList.add(songMap);
}
}
}
但是当我运行应用程序时,我得到了歌曲的名称以及listView上显示的路径。
有没有办法只能显示它的名字,而不能显示它的路径。
答案 0 :(得分:0)
创建自定义列表适配器。
public class SongListAdapter extends ArrayAdapter<SongModel> {
Activity activity;
int layoutResourceId;
List<SongModel> songList = new ArrayList<SongModel>();
public SongListAdapter(Activity activity, int layoutResourceId,
List<SongModel> songList
) {
super(activity, layoutResourceId, songList);
this.layoutResourceId = layoutResourceId;
this.activity = activity;
this.songList = songList;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = ((Activity) activity)
.getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.nameText = (TextView) convertView.findViewById(R.id.nameText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final SongModel songModel = songList.get(position);
holder.nameText.setText(songModel.getName());
return convertView;
}
static class ViewHolder {
TextView nameText;
}
}