我需要列出列表中不同位置的文件,但是第二个对象始终会破坏第一个对象,我很乐意提供帮助

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

        File root = new File(Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera");
        ListDir(root);

        File root1 = new File(Environment.getExternalStorageDirectory().toString() + "/Download" );
        ListDir(root1);
    }

    void ListDir(File f)
    {
        File[] files = f.listFiles();
        fileList.clear();
        for (File file: files)
        {
            fileList.add(file.getName());
        }
        ArrayAdapter<String> dir = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1,fileList);
        setListAdapter(dir);
    }

最佳答案

根据要求的例子

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

        ArrayList<File> roots = new ArrayList<>();

        File root = new File(Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera");
        roots.add(root);

        File root1 = new File(Environment.getExternalStorageDirectory().toString() + "/Download" );
        roots.add(root1);

        //After you've added as many directories as you need
        ListDir(roots);
    }

    void ListDir(ArrayList<File> root_direcs)
    {
        fileList.clear();
        for(File root : root_direcs){
            fileList.addAll(Arrays.asList(root.listFiles()));
        }

        ArrayAdapter<String> dir = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1,fileList);
        setListAdapter(dir);
    }

关于java - 如何在Android中的ListView中显示来自不同位置的两个条目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61030608/

10-09 00:30