2013-05-03 73 views
0

我有一個音樂播放器應用程序,應打開並顯示在您的手機/ SD卡的曲目列表,但是當我啓動應用程序就說明我什麼..音樂播放器庫的ListView填充問題

我也跟着教程等等我不完全清楚所有事情應該做的事情。特別是隨着XML的ListView:http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/

這裏是我的Java類,應該是顯著什麼可能導致此:

Library.java

public class Library extends ListActivity { 
// Songs list 
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.library); 

    ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>(); 

    TrackManager plm = new TrackManager(); 
    // get all songs from sdcard 
    this.songsList = plm.getPlayList(); 

    // looping through playlist 
    for (int i = 0; i < songsList.size(); i++) { 
     // creating new HashMap 
     HashMap<String, String> song = songsList.get(i); 

     // adding HashList to ArrayList 
     songsListData.add(song); 
    } 

    // Adding menuItems to ListView 
    ListAdapter adapter = new SimpleAdapter(this, songsListData, R.layout.libraryitem, new String[] { "songTitle" }, new int[] { R.id.songTitle }); 

    setListAdapter(adapter); 

    // selecting single ListView item 
    ListView lv = getListView(); 
    // listening to single listitem click 
    lv.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      // getting listitem index 
      int songIndex = position; 

      // Starting new intent 
      Intent in = new Intent(getApplicationContext(), Player.class); 
      // Sending songIndex to PlayerActivity 
      in.putExtra("songIndex", songIndex); 
      setResult(100, in); 
      // Closing PlayListView 
      finish(); 
     } 
    }); 
} 

TrackManager.java

public class TrackManager { 
// SDCard Path 
final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory().getPath()); 
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); 

// Constructor 
public TrackManager() { 

} 

// 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()); 

      // Adding each song to SongList 
      songsList.add(song); 
     } 
    } 
    // return songs list array 
    return songsList; 
} 

// Class to filter files which are having .mp3 extension 

class FileExtensionFilter implements FilenameFilter { 
    public boolean accept(File dir, String name) { 
     return (name.endsWith(".mp3") || name.endsWith(".MP3")); 
    } 
} 
} 

我有一些XML佈局,顯示一個列表視圖,這些類應該填充,但我只是得到一個空白的空列表,就像沒有什麼:

Library activity

UPDATE

library.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#111111" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".Library" > 


<TextView 
    android:id="@+id/tvTracks" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/tracks" 
    android:textSize="20sp" /> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@+id/tvTracks" 
    android:divider="#999999" 
    android:dividerHeight="1dp" 
    android:listSelector="@drawable/list_selector" /> 

</RelativeLayout> 

libraryitem.xml爲ListView控件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/list_selector" 
android:orientation="vertical" 
android:padding="5dp" 
tools:ignore="SelectableText" > 

<TextView 
    android:id="@+id/songTitle" 
    android:text="@string/title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="5dp" 
    android:textSize="15sp" /> 

</RelativeLayout> 

希望有人能找出我的問題..

+0

我們可以看到XML爲ListView – j2emanue 2013-05-04 00:10:34

+0

更新問題 – Timmo 2013-05-04 00:18:29

+0

//添加的菜單項到ListView控件 ListAdapter適配器=新SimpleAdapter(這一點,songsListData,R.layout.libraryitem,新的String [] { 「SONGTITLE」},新INT [] {R.id.songTitle}); 爲什麼傳入songsListData這裏不是這個地圖的散列圖嗎?你可以通過songsList來代替,所以我可以看到會發生什麼 – j2emanue 2013-05-04 00:25:09

回答

1

我曾經使用MediaStore本人已對谷歌播放我的音樂應用程序,它似乎在霎那上device.Here所有音頻文件更高效的我抓住所有的音頻文件,爲您提供:

public ArrayList<HashMap<String, String>> getPlayList(Context c) { 



    /*use content provider to get beginning of database query that queries for all audio by display name, path 
    and mimtype which i dont use but got it incase you want to scan for mp3 files only you can compare with RFC mimetype for mp3's 
    */ 
    final Cursor mCursor = c.getContentResolver().query(
      MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.MIME_TYPE }, null, null, 
      "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC"); 

    String songs_name = ""; 
    String mAudioPath = ""; 

    /* run through all the columns we got back and save the data we need into the arraylist for our listview*/ 
    if (mCursor.moveToFirst()) { 
     do { 

     String file_type = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE)); 


      songs_name = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); 
      mAudioPath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); 
      HashMap<String, String> song = new HashMap<String, String>(); 

      song.put("songTitle", songs_name); 
      song.put("songPath", mAudioPath); 

      songsList.add(song); 

     } while (mCursor.moveToNext()); 
    } 

    mCursor.close(); //cursor has been consumed so close it 
return songsList; 
} 

在你的代碼來調用它傳遞一個方面:

this.songsList = plm.getPlayList(this); 

我認爲這是可以接受的方式,你能接受這個答案如果它可以幫助你。

+0

謝謝一切都顯示現在 – Timmo 2013-05-04 08:34:16