我使用简单的适配器填充listview,我找不到如何通过groupData的特定属性订购商品,这是我的代码:
public class LibraryFragment extends Fragment {
private Context mContext;
private ListView listView1;
private ListAdapter adapter;
private List<Map<String, String>> groupData;
private Map<String, String> group;
private String[] from = new String[] {"url", "title", "artist", "duration"};
private int[] to = new int[] {R.id.url, R.id.title, R.id.artist, R.id.duration};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_library, container, false);
mContext = getActivity();
listView1 = (ListView) rootView.findViewById(R.id.listView1);
fillLibrary();
return rootView;
}
public void fillLibrary(){
ArrayList<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
File music = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + Environment.DIRECTORY_MUSIC + "/myapp");
if (music.isDirectory()){
File tracks[] = music.listFiles();
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
for (int i = 0; i < tracks.length; i++) {
File track = tracks[i];
String trackPath = track.getAbsolutePath();
String trackName = track.getName();
String trackExt = trackName.substring(trackName.lastIndexOf('.') + 1);
if(trackExt.equals("MP3")||trackExt.equals("mp3"))
{
mmr.setDataSource(trackPath);
// -- create record
HashMap<String, String> group = new HashMap<String, String>();
group.put( "url", trackPath);
group.put( "title", mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE));
group.put( "artist", mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
group.put( "duration", getDurationString(Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION))/1000));
groupData.add(group);
}
}
Collections.sort(groupData, Functions.mapComparator);
adapter = new SimpleAdapter(mContext , groupData, R.layout.library_item,
from,
to );
listView1.setAdapter( adapter );
}
}
}
这是我的函数Functions.mapComparator
public static Comparator<Map<String, String>> mapComparator = new Comparator<Map<String, String>>() {
public int compare(Map<String, String> m1, Map<String, String> m2) {
return m1.get("title").compareTo(m2.get("title"));
}
};
你知道我做错了吗?
编辑:这是logcat
01-09 20:06:12.005: D/AndroidRuntime(10685): Shutting down VM
01-09 20:06:12.005: W/dalvikvm(10685): threadid=1: thread exiting with uncaught exception (group=0x40d31930)
01-09 20:06:12.065: E/AndroidRuntime(10685): FATAL EXCEPTION: main
01-09 20:06:12.065: E/AndroidRuntime(10685): java.lang.NullPointerException
01-09 20:06:12.065: E/AndroidRuntime(10685): at com.myapp.mobile.Functions$1.compare(Functions.java:44)
01-09 20:06:12.065: E/AndroidRuntime(10685): at com.myapp.mobile.Functions$1.compare(Functions.java:1)
答案 0 :(得分:4)
null
。new Song(url, title, artist, duration);
一样。它可以实现Comparable
,这样您就不再需要Comparator
。答案 1 :(得分:1)
MediaMetadataRetriever.extractMetadata()可以返回null,这就是你得到的。这可能是因为您尝试检索的某些音乐文件没有嵌入标题信息。在添加之前,您应该检查extractMetadata()
的返回值。
一个简单的检查:
String title = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
if (title == null) continue;
将忽略任何没有标题的条目(尽管注意其他属性也可以为null,即使设置了标题)也跳过下一个条目而不添加。