0
我在android中構建音樂播放器應用程序。在單擊listItem時,相應的歌曲將播放,歌曲的名稱將顯示在搜索欄上方的TextView中,其ID爲「selectedItem」。但是數據綁定是在MediaCursorAdapter.class中完成的,它將MediaStore.MediaColumns.DATA作爲字符串返回到MainActivity.java類的onListItemClick()方法。這裏當前歌曲的名字被設置爲字符串。但是這顯示了App中歌曲的整個路徑,看起來很糟糕。有沒有辦法在我的MainActivity.java類中獲取DISPLAY_NAME,或者以某種方式縮短此路徑,以便在應用程序中看起來不那麼糟糕?在Android MediaStore中使用DATA獲取DISPLAY_NAME
MainActivity.java
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
currentFile = (String) v.getTag();
startPlay(currentFile);
}
MediaCursorAdapter.java
@Override
public void bindView(View view, Context context, Cursor cursor) {
//TextView title = (TextView) view.findViewById(R.id.title_name);
TextView displayName = (TextView) view.findViewById(R.id.display_name);
TextView duration = (TextView) view.findViewById(R.id.duration);
String nameOfTheSong = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME));
nameOfTheSong = nameOfTheSong.substring(0,nameOfTheSong.length()-4);
displayName.setText(nameOfTheSong);
//title.setText(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));
long durationInMS = Long.parseLong(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));
double durationInMin = ((double)durationInMS/1000.0)/60.0;
BigDecimal bigDecimal = new BigDecimal(durationInMin);
durationInMin = bigDecimal.setScale(2, RoundingMode.HALF_UP).doubleValue();
duration.setText(""+durationInMin);
//SETTING THE PATH WHICH IS THEN USED IN onListItemClick OF THE MAINACTIVITY.JAVA CLASS
view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}
Github上鍊接:https://github.com/vishwabhat19/PlayMusic.git