0

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
Community
  • 1
  • 1
Tony
  • 277
  • 2
  • 3
  • 10
  • @Tonny: please use Log statement to find out the total number files you have retrieved in your getFromSdCard() and also can you please post the way you have initialize the baseAdapter Object. – Sankar Ganesh PMP Nov 11 '13 at 06:23
  • private MusicAdapter musicAdapter = null; musicAdapter = new MusicAdapter(fileLists); folderList.setAdapter(musicAdapter); – Tony Nov 11 '13 at 06:39
  • Did you check that your ArrayList contains any duplicate values, I mean the fileLists contain any duplicate?, please confirm mate? – Sankar Ganesh PMP Nov 11 '13 at 06:51
  • Sure.. May be that will be the problem. But how to resolve that brother? Don't take it in a wrong way. I'm at the beginner level in android. That's why.. :( – Tony Nov 11 '13 at 06:59
  • No problem, Tony, Please check my answer brother – Sankar Ganesh PMP Nov 11 '13 at 07:14
  • Please see my Updated answer and let me know if you got struck somewhere brother, Thanks – Sankar Ganesh PMP Nov 11 '13 at 07:19
  • Is your problem solved with my answer, if it's solved with the answer, please upvote and accept the answer, so that the other user who having this problem , then they can feel that, they try this answer because it was accepted, please let me know if you still got struck on this problem? – Sankar Ganesh PMP Nov 12 '13 at 04:19
  • Hi brother, I have added my comment in the answer. Sorry for the late reply. I was stuck with some system OS problem. That's why.. :( – Tony Nov 12 '13 at 05:27

1 Answers1

1

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
Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90
  • I'm extremely sorry my brother. My system got stuck on yesterday and was forced to format it once again. That's why I can't be in touch with you. Thanks for your time and help my brother. But, When I use this viewholder, It seems the app is force closing and getting an error message that "Can't get the viewwidth after the first layout" :( Sorry for the late reply :( – Tony Nov 12 '13 at 05:26
  • No problem Tony, can you please post it your stack trace for that View Holder problem by seeing your Logcat? – Sankar Ganesh PMP Nov 12 '13 at 05:49
  • 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 – Tony Nov 12 '13 at 05:54
  • @Tony: can you please post the code in gist, so that i can run in my machine and test it and understand this bug? – Sankar Ganesh PMP Nov 12 '13 at 06:08
  • Can't we do the function without Viewholder?? Form the documentation, i came to know it will be a stimulative to load the contents in the listview. There is one more doubt that, whether this is any logic error in the loop? I think the loop is continuing – Tony Nov 12 '13 at 06:56
  • yes you can do without ViewHolder, because of the advantage of using this pattern , i have recommended this, i was not able to understand your point that's loop is continuing, can you explain which loop has problem and please share the code in gist, so that i can try in my machince – Sankar Ganesh PMP Nov 12 '13 at 07:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40999/discussion-between-tony-and-sankar-ganesh) – Tony Nov 12 '13 at 07:33
  • @Tony: I was not unable to login in this chat, becuase it is not opening for me – Sankar Ganesh PMP Nov 12 '13 at 07:56
  • Sorry to ask brother.. I don't know about gist :( – Tony Nov 12 '13 at 09:27
  • @what is the problem brother? – Sankar Ganesh PMP Nov 14 '13 at 04:33
  • And one more thing brother.. Here filename is not declared by you. So what it will be? I declared it as a string and worked. But of no use. :( But, on y'day after deleting some of my music files, it worked fine and then adter it started the problem. The files are showing again and again. Help me please. The code is the same as that of the above and I have posted it in the above link :( – Tony Nov 15 '13 at 08:08
  • @Tony: the gist link is not working, please tell me what's the problem now? – Sankar Ganesh PMP Nov 15 '13 at 08:40
  • Now also facing the same problem. :( Audio files are repeating in my folder. I tried with your code also. That time 'holder.albumName.setText(fileName.substring(audioFile.get(position).lastIndexOf("/")+1));' showed an error: fileName not specified and I declared it as a string. But, at the time of execution, The app force closes and I don't know what's the reason :( – Tony Nov 15 '13 at 09:17
  • Can you please check my current code?? It's in here as well as in github. The files are showing in the folder; but makes repeatition. That's the problem. :( – Tony Nov 15 '13 at 09:18
  • @Tony: I m sorry i was not able to open that url, sankarganesh.s is my skype id , please come in skype – Sankar Ganesh PMP Nov 15 '13 at 12:53