【编程语言】Android--如何遍历android所有路径

来源:互联网 发布:python3 爬虫 知乎 编辑:程序博客网 时间:2024/06/05 17:46

list = (ListView) findViewById(R.id.music_music_list);

                file = new File("/sdcard/");
                it = new ArrayList<String>();
                searchFiles(file);
                SimpleAdapter ladapter = new SimpleAdapter(this,getLData(),R.layout.music_item, new String[]{"NAME"},new int[]{R.id.music_list_item});
                list.setAdapter(ladapter);
                TextView tv = new TextView(this);
        tv.setText("sd卡中没有音乐文件!");
        tv.setPadding(0, 50, 0, 0);
        addContentView(tv, new LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
        list.setEmptyView(tv);

 

        private void searchFiles(File f) {   //搜索sdcard中的所有MP3文件
                // TODO Auto-generated method stub
                File files[] = f.listFiles();
                for(File tempF:files){
                        if(tempF.isDirectory()){
                                searchFiles(tempF);
                        }
                        else{
                                String path = tempF.getPath();
                                String fpath = path.substring(path.lastIndexOf(".")+1,path.length());
                                if(fpath.equals("mp3")||fpath.equals("wav")){
                                        it.add(path);
                                }
                        }
                }
        }


        private List<? extends Map<String, ?>> getLData() {
                // TODO Auto-generated method stub
                ArrayList<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
                HashMap<String,Object> item;
                int i = 0 ;
                for(i=0;i<it.size();i++){
                        item = new HashMap<String,Object>();
                        String path  = it.get(i).toString();
                        String name = path.substring(path.lastIndexOf("/")+1,path.length());
                        item.put("NAME",name);
                        System.out.println("data"+it.get(i).toString());
                        data.add(item);
                }
                return data;
        }

原创粉丝点击