-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Storage Folders to Adapter #4
Comments
list.add(node);
treeViewAdapter.updateTreeNodes(list); |
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
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 |
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 |
@AmrDeveloper I tried it but Iam getting nullPointerException fails to get …getLayoutId() on a null object reference |
@myusersnamesis One of your nodes is null, try to add check in the crawler function to check if you pass null |
@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;
}
} |
Try to check if
|
@AmrDeveloper Thank you it worked. Another question can I get files located in storage/emulated/0/ as root like if I have: |
You're welcome bro, If you have directory // 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 |
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 and so on... |
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.
The problem is adding the child folders, I don't know how to do this.
The text was updated successfully, but these errors were encountered: