0

我正在用 android 创建自己的音乐播放器。我想将画廊中的所有音乐文件加载到我的文件夹中,以便我可以将其中一些添加到我的收藏夹中。但是,问题是,当我从图库中获取文件时,它会重复。IE。该列表在我的文件夹中显示三次或四次。你能检查一下是否有任何逻辑错误吗?谢谢你 :)

public ArrayList<String> getFromSdcard(String folderName)
{
    ArrayList<String> audioFileLists = new ArrayList<String>();
    File file=  new File(folderName);
    if (file.isDirectory())
{  
    File[] listFile = file.listFiles();//get list of files
        for (int i = 0; i < listFile.length; i++)
{
    String path = listFile[i].getAbsolutePath();
    audioFileLists.add(path);//add path of files to array list
        }
    }
    return audioFileLists;
}

private class MusicAdapter extends BaseAdapter {
    private  ArrayList<String> audioFile;

    public MusicAdapter( ArrayList<String> audioFiles) {
        audioFile = audioFiles;
    }

    public int getCount() {
        return audioFile.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        System.gc();

        LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView =  mInflater.inflate(R.layout.musiclist, null);
        TextView albumName = (TextView) convertView.findViewById(R.id.albumName);           
        String fileName = audioFile.get(position);
        albumName.setText(fileName.substring(fileName.lastIndexOf("/")+1));
        return convertView;
    }
}

这是我尝试使用 ViewHold 后的 logcat。

11-12 11:14:26.043: I/Ads(513): Request scenario: Online server request.
11-12 11:14:27.313: W/webcore(513): Can't get the viewWidth after the first layout
11-12 11:14:27.413: D/webviewglue(513): nativeDestroy view: 0x3053b8
11-12 11:14:27.423: I/Ads(513): onReceiveAd()
11-12 11:14:27.523: D/webviewglue(513): nativeDestroy view: 0x32d048
11-12 11:15:05.163: D/dalvikvm(513): GC_EXPLICIT freed 6162 objects / 466624 bytes in 111ms
11-12 11:28:53.443: D/dalvikvm(513): GC_FOR_MALLOC freed 20535 objects / 582840 bytes in 124ms
11-12 11:42:38.213: D/dalvikvm(513): GC_FOR_MALLOC freed 20821 objects / 523728 bytes in 117ms
11-12 11:56:20.473: D/dalvikvm(513): GC_FOR_MALLOC freed 20837 objects / 524536 bytes in 129ms
4

1 回答 1

1

首先检查您的 ArrayList 即 fileLists 不包含任何重复值,通过以下方式

public ArrayList<String> getFromSdcard(String folderName) {
        ArrayList<String> audioFileLists = new ArrayList<String>();
        File file = new File(Environment.getExternalStorageDirectory()+"/"+folderName);
        if (file.isDirectory()) {
            File[] listFile = file.listFiles();// get list of files
            for (int i = 0; i < listFile.length; i++) {
                String path = listFile[i].getAbsolutePath();
                audioFileLists.add(path);// add path of files to array list

            }
        }
        Log.d("audioFileLists:" + audioFileLists.size(), ""); // Check the Size of the List
        return audioFileLists;
    }

并打印 audioFileLists 中的值以确保您的数组列表不包含重复值

public void print(ArrayList<String> audioFileLists) {
        for (int i = 0; i < audioFileLists.size(); i++) {
            System.out.println("files:" + audioFileLists.get(i));
        }
    }

如果 ArrayList 包含重复项,请使用以下方法从 ArrayList 中删除重复项,我们可以使用 Hashset 删除 ArrayList 的重复项,如下所示。

public ArrayList<String> removeDuplicates(ArrayList<String> audioFileLists){

HashSet<String> listToSet = new HashSet<String>(
                audioFileLists);
        audioFileLists.clear();
        audioFileLists.addAll(listToSet);
       return audioFilesLists;
}

现在您可以再次调用我们上面编写的打印方法以确保删除重复项。如果 ArrayList 没有重复项,则问题在于 Adpater 的使用。

众所周知,适配器的工作是为数据集中的每个元素准备要在 ListView 中显示的视图。这往往涉及许多 findViewById(int) 查找,这些查找在 CPU 时间上非常昂贵,而且我相信这是重复发生的。

Android 的最佳实践规定适配器使用称为 ViewHolder 模式的东西来降低这些查找的成本并消除重复问题。

我在这里解释了如何使用 View Holder 模式。

        public class MusicAdapter extends BaseAdpater{
          private  ArrayList<String> audioFile;
          LayoutInflater mInflater =null;

            public MusicAdapter( ArrayList<String> audioFiles,Context dcontextObject) {
                audioFile = audioFiles;
                mInflater = LayoutInflater.from(dcontextObject);
            }

            public int getCount() {
                return audioFile.size();
            }

            public Object getItem(int position) {
                return position;
            }

            public long getItemId(int position) {
                return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
            // Create an Object for View Holder
            ViewHolder holder;
           if(convertView == null){                 
                convertView =  mInflater.inflate(R.layout.musiclist, null);
                holder.albumName = (TextView) convertView.findViewById(R.id.albumName);  
            }
            else{
                 holder = (ViewHolder) convertView .getTag();
             }                        
                 holder.albumName.setText(fileName.substring(audioFile.get(position).lastIndexOf("/")+1));
                return convertView;
            }// End of getView()

    // create View Holder class here

    class ViewHolder {
            TextView albumName;

        }
  }// End of Adapter
于 2013-11-11T07:07:53.673 回答