Sort list view with Folders and Files types alphabetically in andorid

Question

Sort list view with Folders and Files alphabetically

I am trying the following code

public class MainActivity extends ActionBarActivity {
    private File file;
    private List<String> myList;
    private ListView listView;
    private TextView pathTextView;
    private String mediapath = new String(Environment.getExternalStorageDirectory().getAbsolutePath());

    private final static String[] acceptedExtensions= {"mp3", "mp2",    "wav", "flac", "ogg", "au" , "snd", "mid", "midi", "kar"
        , "mga", "aif", "aiff", "aifc", "m3u", "oga", "spx"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView=(ListView) findViewById(R.id.pathlist);
        pathTextView=(TextView) findViewById(R.id.path);

        myList = new ArrayList<String>();   

        String root_sd = Environment.getExternalStorageDirectory().toString();
        Log.e("Root",root_sd);

        String state = Environment.getExternalStorageState();
        File list[] = null ;
        /* if ( Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) ) {  // we can read the External Storage...
            list=getAllFilesOfDir(Environment.getExternalStorageDirectory());
        }*/

        pathTextView.setText(root_sd);

        file = new File( root_sd ) ;       
        list = file.listFiles(new AudioFilter());
        Log.e("Size of list ","" +list.length);
        //LoadDirectory(root_sd);

        for( int i=0; i< list.length; i++)
        {

            String name=list[i].getName();
        int count =     getAudioFileCount(list[i].getAbsolutePath());
       Log.e("Count : "+count, list[i].getAbsolutePath());
       if(count!=0)
       myList.add(name);
            /*int count=getAllFilesOfDir(list[i]);
            Log.e("Songs count ",""+count);

            */  

        }


        listView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, myList ));

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                File temp_file = new File( file, myList.get( position ) );  

                if( !temp_file.isFile())        
                {
                    //LoadDirectory(myList.get( position ));

                    file = new File( file, myList.get( position ));
                    File list[] = file.listFiles(new AudioFilter());

                    myList.clear();

                    for( int i=0; i< list.length; i++)
                    {
                        String name=list[i].getName();

                        int count =     getAudioFileCount(list[i].getAbsolutePath());
                           Log.e("Count : "+count, list[i].getAbsolutePath());
                           if(count!=0)
                          myList.add(name);
                        /*int count=getAllFilesOfDir(list[i]);
                        Log.e("Songs count ",""+count);
                        if(count!=0)
                            myList.add(name);*/
                    }

                    pathTextView.setText( file.toString());
                    //Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_LONG).show(); 
                    listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, myList ));

                }


            }
        });



    }

It gives me the files and folders for all songs

Howevever ,If a folder contains both sub folders and songs ,I want to sort the listview to show all sub folders first and then all the songs in that folder

How can this be done


Show source
| sorting   | android   | listview   2017-01-06 11:01 2 Answers

Answers to Sort list view with Folders and Files types alphabetically in andorid ( 2 )

  1. 2017-01-06 11:01

    use this function

    Collections.sort(myList);
    

    add this line before setAdapter.

    it will sort your String Arraylist in Alphabet order.

  2. 2017-01-06 11:01

    Try the following code to sort:

    Collections.sort(list, new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {
            return s1.compareToIgnoreCase(s2);
        }
    });
    

Leave a reply to - Sort list view with Folders and Files types alphabetically in andorid

◀ Go back