Waggoner_Keith
Waggoner_Keith

Reputation: 600

ListView adds same information twice

I have listview an i am populating it with songs from the sd card. When there are more than one song then it just adds the same information for all the songs. So all the song titles are the same and everything.

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


        }

Upvotes: 0

Views: 187

Answers (1)

Tobias
Tobias

Reputation: 7415

try using:

if(cursor.movetoFirst()) {

    do {

    // all your cursor accessing code

    while(cursor.moveToNest());

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

}

instead of your for-loop

Upvotes: 1

Related Questions