抱歉,如果我在这里遗漏了一些非常简单的东西,我对编程很新。
我正在尝试构建一个媒体播放器,并发现了使用MediaStore.Audio收集播放列表媒体的示例。 我现在需要实现一个适配器,将其转换为listview供用户选择。 我认为这可能是实现简单曲目列表的最优雅方式。
到目前为止我有:
SimpleCursorAdapter myAdapter; String[] STAR = { "*" }; Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; Cursor cursor = getContentResolver().query(allsongsuri, STAR, selection, null, null); if (cursor != null) { if (cursor.moveToFirst()) { do { String song_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)); int song_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID)); String fullpath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); String album_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)); int album_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); String artist_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); int artist_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID)); } while (cursor.moveToNext()); } cursor.close(); } }现在我想使用simpleCursorAdaptor listView,但不知道如何去做,或者把它放在哪里。 任何帮助将不胜感激。
Apologies if I am missing something very simple here, I am very new to programming.
I am trying to build a media player, and have discovered and example of the use of MediaStore.Audio to collect media for a playlist. I now need to implement an adapter to get this into a listview for the user to select. I am under the impression this is probably the most elegant way of implementing a simple track list.
So far I have:
SimpleCursorAdapter myAdapter; String[] STAR = { "*" }; Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; Cursor cursor = getContentResolver().query(allsongsuri, STAR, selection, null, null); if (cursor != null) { if (cursor.moveToFirst()) { do { String song_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)); int song_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID)); String fullpath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); String album_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)); int album_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); String artist_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); int artist_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID)); } while (cursor.moveToNext()); } cursor.close(); } }Now I am looking to use a simpleCursorAdaptor to listView, but not sure how to go about it, or where to put it. Any help will be greatly appreciated.
最满意答案
您必须像使用“活动”一样实现自己的SimpleCursorAdapter。
MediaStoreCursorAdapter extends SimpleCursorAdapter { . . .要将它分配给ListView,请在OnCreate of Activity via中获取对它的引用
ListView myListView = (ListView) findViewById(R.id.mylistview);然后将适配器分配给它:
myListView.setAdapter(myAdapter);如果您是初学者,您可能需要查看一些基础教程,以便在深入学习之前更好地了解语言和框架。
祝你好运!
You will have to implement your own SimpleCursorAdapter like you do it with Activities.
MediaStoreCursorAdapter extends SimpleCursorAdapter { . . .To assign it to your ListView get a reference to it in OnCreate of Activity via
ListView myListView = (ListView) findViewById(R.id.mylistview);And then assign the Adapter to it:
myListView.setAdapter(myAdapter);If youre a beginner you might want to look into some basic tutorials to get to know language and framework a little better before diving deeper.
Good Luck!
更多推荐
发布评论