我可以列出媒体商店中的所有歌曲,但我也想按艺术家/专辑/文件夹/流派/播放列表对它们进行排序。
我通常使用游标查询自己的数据库,但这在这种情况下无效,我必须使用适配器。我可以通过更改参数来查询它,但我不知道如何在适配器中对其进行分组(我找不到如何添加/使用 GROUPBY),这意味着我需要适配器中的每个艺术家/专辑/文件夹/..只有一次,不是每首歌。
我试图通过互联网搜索,但没有找到答案。我也在这里找到了一些主题,但对我来说仍然不清楚。因此,我想请您在此示例中向我展示:
package com.exercise.AndroidListMedia;
import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.Media;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class AndroidListMediaActivity extends ListActivity {
SimpleCursorAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = {
MediaStore.MediaColumns.TITLE};
int[] to = {
android.R.id.text1};
Cursor cursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null,
null,
null,
MediaStore.Audio.Media.TITLE);
adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, from, to);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor cursor = adapter.getCursor();
cursor.moveToPosition(position);
String _id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
int duration = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
String info = "_ID: " + _id + "\n"
+ "TITLE: " + title + "\n"
+ "ARTIST: " + artist + "\n"
+ "ALBUM: " + album + "\n"
+ "DURATION: " + duration/1000 + "s";
Toast.makeText(this, info, Toast.LENGTH_LONG).show();
}
}
谢谢你的任何想法。