2011-11-20 50 views
0

我有listview我填充它從SD卡的歌曲。當有多首歌曲時,它只會爲所有歌曲添加相同的信息。所以所有的歌曲標題都是一樣的。ListView兩次添加相同的信息

if(cursor.moveToFirst()){ 
     for(int j=0;j<cursor.getCount();j++){ 

      int ALBUM_ID = cursor.getInt((cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ID))); 

      int pathcolumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA); 

      String path1 = cursor.getString(pathcolumn); 

      String album_url = null; 

      Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); 
      Uri uri = ContentUris.withAppendedId(sArtworkUri, ALBUM_ID); 

      album_url = uri.toString(); 
      ContentResolver res = this.getContentResolver(); 
       // Album 
       String album_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM)); 

       String year = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.YEAR)); 
       // String year = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS)); 


       // artist 
       String artist_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.ArtistColumns.ARTIST)); 
       // display name 
       String DisplayName = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)); 

       //title 
       String Title = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)); 

       songtitle.add(Title); 
       artistname.add(artist_name); 
       albumname.add(album_name); 
       path.add(path1); 

      } 
      } 
       adapter = new ArrayAdapter<String>(this,R.layout.song,songtitle); 

       setListAdapter(adapter); 


     } 

回答

1

嘗試使用:

if(cursor.movetoFirst()) { 

    do { 

    // all your cursor accessing code 

    while(cursor.moveToNest()); 

    adapter = new ArrayAdapter<String>(this,R.layout.song,songtitle); 
    setListAdapter(adapter); 

} 

,而不是你的for循環

+0

謝謝你的男人,它的工作,你能解釋一下爲什麼我還努力學習,我會給你在3分鐘的代表 –

+0

您必須調用cursor.moveToNext或Cursor.movetoPosition(X)dor訪問不同的數據集。 –

+0

好的,謝謝你 –

相關問題