好的,我正在为 android 制作一个媒体播放器应用程序。直到现在一切都很好。到目前为止,我有一个列表视图,显示你的 sdcard(内部和外部)上的所有 .mp3 文件,并且在播放时显示音乐可视化器。但我不能为我的生活按字母顺序排列列表。一切都是动态的,所以 xml 在这里不起作用。
public class MusicPlayerActivity extends Activity {
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
private Intent aIntent;
public static String filename;
private RelativeLayout mRelativeLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init_phone_music_grid();
mRelativeLayout = new RelativeLayout(this);
setContentView(mRelativeLayout);
mRelativeLayout.addView(musiclist);
}
public void init_phone_music_grid() {
System.gc();
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ALBUM
};
Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
musiccursor = managedQuery(allsongsuri, projection , selection, null, null);
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.PhoneMusicList);
musiclist.setAdapter(new EfficientAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,long id) {
System.gc();
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
filename = musiccursor.getString(music_column_index);
aIntent = new Intent(v.getContext(), AudioFX.class);
aIntent.getStringExtra(filename);
startActivity(aIntent);
}
};
class EfficientAdapter extends BaseAdapter {
private Context mContext;
public EfficientAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
String id = null;
TextView tv;
if (convertView == null) {
tv = new TextView(mContext.getApplicationContext());
} else{
tv = (TextView) convertView;
}
musiccursor.moveToPosition(position);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
id = musiccursor.getString(music_column_index);
tv.setText(id);
tv.setTextSize(20);
return tv;
}}}