//读取歌曲列表 List<Map<String,Object>>nameList = new ArrayList<Map<String,Object>>(); int i=1; for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext()){ cur.getColumnCount(); String tempName = cur.getString(2); Map<String,Object> map = new HashMap<String,Object>(); map.put("ItemText", i+blank+tempName); map.put("ItemImage", R.drawable.icon); map.put("ItemTitle", ""); nameList.add(map); i++;
//添加到musicInfo MusicInfo info = new MusicInfo(); info.setName(tempName); info.setPath(cur.getString(1)); infoList.add(info); } //填充歌曲列表 SimpleAdapter adapter = new SimpleAdapter(this,nameList,R.layout.forlist, new String[] {"ItemImage","ItemTitle", "ItemText"}, new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}); setListAdapter(adapter);
} return super.onMenuItemSelected(featureId, item); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { //播放音乐 Object item = l.getAdapter().getItem(position); Map<String,Object> map = (Map<String,Object>)item; String name = String.valueOf(map.get("ItemText")); name = name.substring(name.indexOf(blank)); String path = MusicInfo.findMucByName(infoList, name).getPath(); MusicInfo.playMuc(Act_LocalStore.this,path); }
http://www.javaeye.com/topic/540423
import java.util.HashMap;
import java.util.List;
import java.util.Map;import com.tianlai.music.MusicInfo;import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;public class Act_LocalStore extends ListActivity {
/** Called when the activity is first created. */
List<MusicInfo> infoList = null;
/**播放列表空格*/
private static String blank="_ ";
@Override
public void onCreate(Bundle savedInstanceState) {
infoList =new ArrayList<MusicInfo>();
super.onCreate(savedInstanceState);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("Act_LocalStore-onStart");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("Act_LocalStore-onPause");
} @Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("Act_LocalStore-onResume");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1, 1, 0, R.string.scan_muc);
SubMenu sm_pmodel = menu.addSubMenu(R.string.play_model);
sm_pmodel.add(2, 21, 1, R.string.byorder);
sm_pmodel.add(2, 22, 2, R.string.single_cicle);
sm_pmodel.add(2, 23, 3, R.string.all_cicle);
sm_pmodel.add(2, 24, 4, R.string.byorder);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if(item.getItemId() ==1){
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
//读取歌曲列表
List<Map<String,Object>>nameList = new ArrayList<Map<String,Object>>();
int i=1;
for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext()){
cur.getColumnCount();
String tempName = cur.getString(2);
Map<String,Object> map = new HashMap<String,Object>();
map.put("ItemText", i+blank+tempName);
map.put("ItemImage", R.drawable.icon);
map.put("ItemTitle", "");
nameList.add(map);
i++;
//添加到musicInfo
MusicInfo info = new MusicInfo();
info.setName(tempName);
info.setPath(cur.getString(1));
infoList.add(info);
}
//填充歌曲列表
SimpleAdapter adapter = new SimpleAdapter(this,nameList,R.layout.forlist,
new String[] {"ItemImage","ItemTitle", "ItemText"},
new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText});
setListAdapter(adapter);
}
return super.onMenuItemSelected(featureId, item);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//播放音乐
Object item = l.getAdapter().getItem(position);
Map<String,Object> map = (Map<String,Object>)item;
String name = String.valueOf(map.get("ItemText"));
name = name.substring(name.indexOf(blank));
String path = MusicInfo.findMucByName(infoList, name).getPath();
MusicInfo.playMuc(Act_LocalStore.this,path);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip"
android:paddingTop="5dip"
>
<ImageView
android:layout_alignParentRight="true"
android:layout_width="19dip"
android:layout_height="38dip"
android:id="@+id/ItemImage"
android:paddingTop="0dip"
/>
<TextView
android:layout_height="wrap_content"
android:textSize="0dip"
android:layout_width="fill_parent"
android:id="@+id/ItemTitle"
android:layout_alignBaseline="@id/ItemImage"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18dip"
android:id="@+id/ItemText"
android:layout_alignBaseline="@id/ItemImage"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/id_playlist" />