package com.GoodG.app.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.util.Log;



public class GetMusicDB {
	
	
	Context context;
	public List<Mp3Info> getMp3Infos(Context context) {
		this.context = context;
		Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		List<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
		Mp3Info mp3Info = new Mp3Info();
		
		while(cursor.moveToNext()) {
			if( cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)) != 0) {
				continue;
			}
			mp3Info.setId(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)) );  //ID
			Log.d("music","music id id " + mp3Info.getId());
			mp3Info.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)) ); //
			Log.d("musict","music title is " + mp3Info.getTitle());
			
			mp3Info.setArtist(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)) ); //
			
			mp3Info.setDuration(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)) );//ʱ  
			
		    mp3Info.setSize(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)) ); //ļС  
		    
		    mp3Info.setUrl(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)) );  //ļ·  
		    
		    mp3Infos.add(mp3Info);
		}
		return mp3Infos;
	}
	
	/**
	 * б
	 * @param mp3Infos
	 */
	public List<HashMap<String, String>> setListAdpter(List<Mp3Info> mp3Infos) {
		List<HashMap<String, String>> mp3list = new ArrayList<HashMap<String, String>>();
		for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
			Mp3Info mp3Info = (Mp3Info) iterator.next();
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("title", mp3Info.getTitle());
			map.put("Artist", mp3Info.getArtist());
			map.put("duration", String.valueOf(mp3Info.getDuration()));
			map.put("size", String.valueOf(mp3Info.getSize()));
			map.put("url", mp3Info.getUrl());
			mp3list.add(map);
			
		}
		return mp3list;
	
	}


}

@@ -308,24 +308,31 @@
 xt;%0A%09public 
+static 
 List%3CMp3Info
@@ -368,34 +368,8 @@
 ) %7B%0A
-%09%09this.context = context;%0A
 %09%09Cu
@@ -522,16 +522,17 @@
 DER);%0A%09%09
+ 
 List%3CMp3
@@ -579,75 +579,83 @@
 );%0A%09
-%09Mp3Info mp3Info = new Mp3Info();%0A%09%09%0A%09%09while(cursor.moveToNext()) %7B
+%0A%0A%09%09if(cursor.moveToFirst()) %7B%0A%0A%09%09%09do %7B%0A%09%09%09Mp3Info mp3Info = new Mp3Info();
 %0A%09%09%09
@@ -732,9 +732,9 @@
 C)) 
-!
+=
 = 0)
@@ -853,25 +853,25 @@
 D%0A%09%09%09Log.d(%22
-m
+M
 usic%22,%22music
@@ -1009,14 +1009,13 @@
 .d(%22
-m
+M
 usic
-t
 %22,%22m
@@ -1052,32 +1052,168 @@
 itle());%0A%09%09%09%0A%09%09%09
+if(%22%3Cunknown%3E%22.equals(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))) )%7B%0A%09%09%09%09mp3Info.setArtist(%22%DE%B4%22);%0A%09%09%09%7D else  
 mp3Info.setArtis
@@ -1295,16 +1295,20 @@
  //%0A%09%09%09%0A
+%09%09%09%0A
 %09%09%09mp3In
@@ -1648,19 +1648,71 @@
 3Info);%0A
+%0A%09
 %09%09%7D
+ while(cursor.moveToNext());%0A%09%09%7D%0A%09%09cursor.close();
 %0A%09%09retur
@@ -1772,16 +1772,23 @@
 %09public 
+static 
 List%3CHas
@@ -2376,16 +2376,78 @@
 d(map);%0A
+%09%09%09Log.d(%22hashmap%22,%22music title aaa is %22+mp3Info.getTitle());%0A
 %09%09%09%0A%09%09%7D%0A

修改加载歌曲列表重复的bug，优化歌曲显示界面.