ListView不会更新

发布于 2024-12-16 18:28:46 字数 2054 浏览 2 评论 0原文

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

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

谢谢您的帮助。

When I run my class the list view is populated with one song the first time but since then I have add songs and they won't add to the list view only the first one is there. I think that my activity is not restarting every time it closes. Here is my update method:

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

And here is my destroy method

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

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飞烟轻若梦 2024-12-23 18:28:47

看一下使用 CursorAdapter 或 < a href="http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html" rel="nofollow">SimpleCursorAdapter。当您已经使用光标时,它可以让您的生活变得更加轻松。

Take a look at using CursorAdapter or SimpleCursorAdapter. It can make your life much easier when you're already working with Cursors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文