Question

I was on the way of creating a music player of my own in android. I want ot load all the music files from gallery to my folder so that I can add some of them to my favourites. But, the problem is, while I'm getting the files from gallery it makes repeatition. ie. the list is showing thrice or four times in my folder. Can you please check whether ther is any logic error?? Thank You :)

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;
    }
}

Here is my logcat after trying with ViewHold.

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
Was it helpful?

Solution

First check wether your ArrayList i.e., fileLists does not contains any duplicate values ,by the following

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;
    }

and print the Values in audioFileLists to ensure that your arraylist does not contain duplicate values

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

if the ArrayList contain duplicate please use the following method to remove from duplicates from the ArrayList, we can remove the duplicates of ArrayList using Hashset as i shown below.

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

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

and now again you can call print method which we wrote above to ensure the duplicates are removed.if the ArrayList does not have duplicate then the problem is with usage of Adpater.

As well knew, the adapter’s job is to prepare the views to be shown in the ListView for each element in the dataset. This tends to involve many findViewById(int) lookups which are quite costly in CPU time and also i believe that here's the duplication occurs.

The standing Best Practices for Android dictate that adapters use something called the ViewHolder Pattern to mitigate the cost of these lookups and also to eliminate the duplication issue.

I have explained here, how to use View Holder pattern.

        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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top