Android:在可点击的列表视图中按标题对mp3文件进行排序

Android:在可点击的列表视图中按标题对mp3文件进行排序,android,listview,sorting,mp3,Android,Listview,Sorting,Mp3,我正在尝试为Nexus 7平板电脑创建音乐播放器应用程序。我能够从特定目录中检索音乐文件以及与之相关的信息,如标题、艺术家等。我已将文件标题加载到可单击的列表视图中。当用户单击标题时,它会将他们带到播放相关歌曲的活动。我试图按字母顺序对标题进行排序,但遇到了一个障碍。当我只对标题进行排序时,它们不再与正确的歌曲匹配。这是意料之中的,因为我只订购了标题,而不是实际的文件。我尝试通过以下操作修改排序算法: //will probably only work for Nexus 7 private f

我正在尝试为Nexus 7平板电脑创建音乐播放器应用程序。我能够从特定目录中检索音乐文件以及与之相关的信息,如标题、艺术家等。我已将文件标题加载到可单击的列表视图中。当用户单击标题时,它会将他们带到播放相关歌曲的活动。我试图按字母顺序对标题进行排序,但遇到了一个障碍。当我只对标题进行排序时,它们不再与正确的歌曲匹配。这是意料之中的,因为我只订购了标题,而不是实际的文件。我尝试通过以下操作修改排序算法:

//will probably only work for Nexus 7
private final static File fileList = new File("/storage/emulated/0/Music/");
private final static File fileNames[] = fileList.listFiles(); //get list of files

public static void sortFiles()
{
    int j;
    boolean flag = true;
    File temp;

    MediaMetadataRetriever titleMMR = new MediaMetadataRetriever();
    MediaMetadataRetriever titleMMR2 = new MediaMetadataRetriever();

     while(flag)
     {
         flag = false;

         for(j = 0; j < fileNames.length - 1; j++)
         {
             titleMMR.setDataSource(fileNames[j].toString());
             titleMMR2.setDataSource(fileNames[j+1].toString());

             if(titleMMR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE).compareToIgnoreCase(titleMMR2.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE)) > 0)
             {
                 temp = fileNames[j];
                 fileNames[j] = fileNames[j+1];     // swapping
                 fileNames[j+1] = temp;
                 flag = true;
             }//end if 
         }//end for
     }//end while
}
该片段还导致了崩溃。java代码中没有错误,所有适当的类都已导入。我真不知道出了什么事。任何帮助都将不胜感激,如果需要任何新信息,我将乐意提供。提前谢谢

固定的

正确的代码剪辑如下:

public static void sortFiles()
{
    int j;
    boolean flag = true;
    File temp;

    MediaMetadataRetriever titleMMR = new MediaMetadataRetriever();
    MediaMetadataRetriever titleMMR2 = new MediaMetadataRetriever();

     while(flag)
     {
         flag = false;

         for(j = 0; j < fileNames.length - 1; j++)
         {
             titleMMR.setDataSource(fileNames[j].toString());
             titleMMR2.setDataSource(fileNames[j+1].toString());

             String title1;
             String title2;

             if(titleMMR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE) == null)
                 title1 = fileNames[j].getName();
             else
                 title1 = titleMMR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);

             if(titleMMR2.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE) == null)
                 title2 = fileNames[j+1].getName();
             else
                 title2= titleMMR2.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);

             if(title1.compareToIgnoreCase(title2) > 0)
             {
                 temp = fileNames[j];
                 fileNames[j] = fileNames[j+1];     // swapping
                 fileNames[j+1] = temp;
                 flag = true;
             }//end if 
         }//end for
     }//end while
}

当你尝试使用定制比较器时,log cat中的错误是什么?它崩溃了?我在Nexus 7平板电脑上运行该应用程序。编译完apk后,我退出模拟器并将其加载到平板电脑。我不知道用Eclipse模拟文件系统的方法,因此我没有任何错误消息。我想因为我使用平板电脑上的文件来获取信息,模拟器就不能工作了。很抱歉给您带来不便,我发现了我的问题。它不在任何代码中。我的一个MP3没有标题集。我并没有解释这一点,而是试图拉一个不存在的标题。我将编辑OP以修复它。
public static void sortFiles()
{
    int j;
    boolean flag = true;
    File temp;

    MediaMetadataRetriever titleMMR = new MediaMetadataRetriever();
    MediaMetadataRetriever titleMMR2 = new MediaMetadataRetriever();

     while(flag)
     {
         flag = false;

         for(j = 0; j < fileNames.length - 1; j++)
         {
             titleMMR.setDataSource(fileNames[j].toString());
             titleMMR2.setDataSource(fileNames[j+1].toString());

             String title1;
             String title2;

             if(titleMMR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE) == null)
                 title1 = fileNames[j].getName();
             else
                 title1 = titleMMR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);

             if(titleMMR2.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE) == null)
                 title2 = fileNames[j+1].getName();
             else
                 title2= titleMMR2.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);

             if(title1.compareToIgnoreCase(title2) > 0)
             {
                 temp = fileNames[j];
                 fileNames[j] = fileNames[j+1];     // swapping
                 fileNames[j+1] = temp;
                 flag = true;
             }//end if 
         }//end for
     }//end while
}