AmrDeveloper / TreeView

Multi roots TreeView :palm_tree: implementation for Android Platform with a lot of options and customization
https://amrdeveloper.github.io/TreeView/
MIT License
174 stars 23 forks source link

Storage Folders to Adapter #4

Open friostd opened 2 years ago

friostd commented 2 years ago

How do I get all the storage folders and add them to the treeViewAdapter? I've tried all sorts of ways, but I can't.

File root = new File("/storage/emulated/0/");
for (File file : root.listFiles()) {
    TreeNode node = new TreeNode(file.getName(), R.layout.layout);
}

The problem is adding the child folders, I don't know how to do this.

friostd commented 2 years ago
list.add(node);

treeViewAdapter.updateTreeNodes(list);
AmrDeveloper commented 2 years ago

Hello @FrioGitHub

Your code will only get the first level of folders for example if you run it with path of D0, you will get D1 and D2 Only

D0
  D1
    - F1
    - F2
    - F3
  D2
    - F4
    - F5
    - F6

To get all storge folders and files you need to create a file crawler for example

public TreeNode crawlerStorageFiles(File parentPath) {
    if (parentPath.isDirectory())  {
        TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
        for (File file : parentPath.listFiles()) {
            node.addChild(crawlerStorageFiles(file));
        }
        return node;
    } else {
        TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
        return node;
    }
}

This code will first get Start from D0 and find D1 if it files it will return it and if it directory it will create a node and recursion on it to get all of children

and on the end, you will add it to the list of roots and add it to the adapter

TreeNode root = crawlerStorageFiles(new File("/storage/emulated/0/"));
list.add(node);
treeViewAdapter.updateTreeNodes(list);
friostd commented 2 years ago

Hello @FrioGitHub

Your code will only get the first level of folders for example if you run it with path of D0, you will get D1 and D2 Only

D0
  D1
    - F1
    - F2
    - F3
  D2
    - F4
    - F5
    - F6

To get all storge folders and files you need to create a file crawler for example

public TreeNode crawlerStorageFiles(File parentPath) {
    if (parentPath.isDirectory())  {
        TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
        for (File file : parentPath.listFiles()) {
            node.addChild(crawlerStorageFiles(file));
        }
        return node;
    } else {
        TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
        return node;
    }
}

This code will first get Start from D0 and find D1 if it files it will return it and if it directory it will create a node and recursion on it to get all of children

and on the end, you will add it to the list of roots and add it to the adapter

TreeNode root = crawlerStorageFiles(new File("/storage/emulated/0/"));
list.add(node);
treeViewAdapter.updateTreeNodes(list);

bro, it doesn't work with files. it only shows folders

AmrDeveloper commented 2 years ago

Hello @FrioGitHub,

If you passed a path for one file it will return it as TreeNode,

If you have one folder with many files you should pass the path of this folder not a single file to get all of them

myusersnamesis commented 2 years ago

@AmrDeveloper I tried it but Iam getting nullPointerException fails to get …getLayoutId() on a null object reference

AmrDeveloper commented 2 years ago

@myusersnamesis One of your nodes is null, try to add check in the crawler function to check if you pass null

myusersnamesis commented 2 years ago

@AmrDeveloper like this?

public TreeNode crawlerStorageFiles(File parentPath) {
    if (parentPath.isDirectory())  {
        TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
        If (!parentPath == null){
           for (File file : parentPath.listFiles()) {
            node.addChild(crawlerStorageFiles(file));
           }
       }
        return node;
    } else {
        TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
        return node;
    }
}
AmrDeveloper commented 2 years ago

Try to check if crawlerStorageFiles return null in any stage for example

public TreeNode crawlerStorageFiles(File parentPath) {
    if (parentPath.isDirectory())  {
        TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
        If (!parentPath == null){
           for (File file : parentPath.listFiles()) {
               TreeNode n = crawlerStorageFiles(file);
               if (n == null) {
                  Log.d(TAG, "Node is null);
               }
               node.addChild(n);
           }
       }
        return node;
    } else {
        TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
        return node;
    }
}
myusersnamesis commented 2 years ago

@AmrDeveloper Thank you it worked. Another question can I get files located in storage/emulated/0/ as root like if I have: 0 downloads

AmrDeveloper commented 2 years ago

You're welcome bro,

If you have directory 0 the straightforward solution is to do this

// This will loop on downloads, android...etc
for (File file : zeroDirectory.listFiles()) {
   TreeNode zeroSubFile = crawlerStorageFiles(file);
}

This code will give you a list of zero sub roots which are what you want [downloads, android] with their children's

kisonix commented 1 year ago

How to implement feature for inserting new item and remove existing item(delete/creat new file/folder) without effecting treeview state for some following methods

https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemInserted(int)

https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemRemoved(int)

and so on...