我的模拟器中有9首歌曲,列表视图中显示的项目数量为9.好极了,太棒了!!!唯一的问题是这9首歌是同一首歌。我一直在这个网站上找不到我的问题的解决方案。下面的代码是我用来查询媒体商店的代码。
package javierpech.codeit.xaverius;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
public class queryMediaStore {
private ArrayList<HashMap<String, String>> songs= new ArrayList<HashMap<String, String>>();
private String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
private Uri externalUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
private Cursor cursor;
private String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
private String[] projection = {
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
// MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DATA,
// MediaStore.Audio.Media.DURATION
};
//PUBLIC CONSTRUCTOR
public queryMediaStore(){
}
public ArrayList<HashMap<String, String>> updatePlaylist(Context c){
HashMap<String, String> tempSong = new HashMap<String, String>();
cursor = c.getContentResolver().query(
externalUri,
projection,
selection,
null,
sortOrder);
if (cursor!= null)
{
if(cursor.moveToFirst()){
while(cursor.moveToNext()){
tempSong.put("songArtist", cursor.getString(0));
tempSong.put("songTitle", cursor.getString(1));
tempSong.put("songPath", cursor.getString(2));
songs.add(tempSong);
}
}
}cursor.close();
return songs;
}
}
答案 0 :(得分:0)
尝试这个来获取歌曲
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
public ArrayList<HashMap<String, String>> getPlayList(Context c) {
final Cursor mCursor = c.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaColumns.TITLE, MediaColumns.DATA, AudioColumns.ALBUM }, null, null,
"LOWER(" + MediaColumns.TITLE + ") ASC");
String songTitle = "";
String songPath = "";
/* run through all the columns we got back and save the data we need into the arraylist for our listview*/
if (mCursor.moveToFirst()) {
do {
songTitle = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaColumns.TITLE));
songPath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaColumns.DATA));
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", songTitle);
song.put("songPath", songPath);
songsList.add(song);
} while (mCursor.moveToNext());
}
mCursor.close(); //cursor has been consumed so close it
return songsList;
}
然后将歌曲添加到列表中
public class PlayListActivity extends ListActivity {
// Songs list
public ArrayList<HashMap<String,String>> songsList = new ArrayList<HashMap<String, String>>();
ListView musiclist;
Cursor mCursor;
int songTitle;
int count;
int songPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
ArrayList<HashMap<String, String>> tempSong = new ArrayList<HashMap<String, String>>();
SongsManager plm = new SongsManager();
// get all songs from sdcard
this.songsList = plm.getPlayList(this);
// looping through playlist
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
HashMap<String, String> song = songsList.get(i);
// adding HashList to ArrayList
tempSong.add(song);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, tempSong,
android.R.layout.simple_list_item_1, new String[] { "songTitle", "songPath" }, new int[] {
android.R.id.text1});
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting listitem index
int songIndex = position;
// Starting new intent
Intent in = new Intent(getApplicationContext(),
MainActivity.class);
Log.d("TAG","onItemClick");
// Sending songIndex to PlayerActivity
in.putExtra("songPath", songIndex);
setResult(100, in);
// Closing PlayListView
finish();
}
});
}
可能有人有更好看的方式,但我知道这100%有效!