ListView不会更新

时间:2011-11-20 08:40:13

标签: android listview listadapter ondestroy

当我运行我的课程时,列表视图第一次填充了一首歌曲,但从那时起我添加了歌曲,他们不会添加到列表视图中,只有第一个在那里。我认为我的活动不会在每次关闭时重新启动。这是我的更新方法:

public void updatelist() {

    Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,null);

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

这是我的破坏方法

@Override
protected void onDestroy() {
    super.onDestroy();
adapter.clear();
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

查看使用CursorAdapterSimpleCursorAdapter。当您使用Cursor时,它可以让您的生活更轻松。