2014-02-24 97 views
3

我在我的模擬器中有9首歌曲,列表視圖中出現的項目數量爲9個。好極了,太棒了!唯一的問題是這9首歌是同一首歌。我一直在繞過這個網站,並沒有找到解決我的問題。下面的代碼是我用來查詢媒體商店的。如何從mediastore獲取歌曲?

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

嘗試這樣來的歌曲

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%這是有效的!