【问题标题】:Sort list view with Folders and Files types alphabetically in andorid在android中按字母顺序对具有文件夹和文件类型的列表视图进行排序
【发布时间】:2017-01-06 10:15:43
【问题描述】:

按字母顺序排列文件夹和文件的列表视图

我正在尝试以下代码

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

                }


            }
        });



    }

它给了我所有歌曲的文件和文件夹

但是,如果一个文件夹同时包含子文件夹和歌曲,我想对列表视图进行排序以首先显示所有子文件夹,然后是该文件夹中的所有歌曲

如何做到这一点

【问题讨论】:

  • 您如何区分文件和文件夹
  • 它检查 temp_file 中的文件夹或文件 if( !temp_file.isFile())
  • 如果是文件夹就会进入这个if条件?那么文件呢?

标签: android sorting listview


【解决方案1】:

使用这个功能

Collections.sort(myList);

setAdapter 之前添加这一行。

它将按字母顺序对您的字符串数组列表进行排序。

【讨论】:

  • 是的,但是我们如何排序以仅显示文件夹,然后显示同一列表视图中的文件
【解决方案2】:

试试下面的代码排序:

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

【讨论】:

    猜你喜欢
    • 2014-08-15
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 2017-11-19
    相关资源
    最近更新 更多