Eventually, I found a blog post by Android-er, who shows a very simple version of the mechanics behind a file open dialog. After getting my version of his code up and running, I wanted to make a few modifications to meet my needs. These modifications are:
- Alphabetize the list of files
- Exclude hidden files, hidden directories, and directories without read access
- Only show files with compatible file extensions
File[] files = f.listFiles();
I create a FileFilter to filter out the results:
File[] files = f.listFiles(new FileFilter() {@Overridepublic boolean accept(File pathname){//If a file or directory is hidden, or unreadable, don't show it in the list.if(pathname.isHidden())return false;if(!pathname.canRead())return false;//Show all directories in the list.if(pathname.isDirectory())return true;//Check if there is a supported file type that we can read.String fileName = pathname.getName();String fileExtension;int mid= fileName.lastIndexOf(".");fileExtension = fileName.substring(mid+1,fileName.length());for(String s : supportedFileExtensions) {if(s.contentEquals(fileExtension)) {return true;}}return false;}});
The last change was to sort the files and directories alphabetically. I do the sorting operations after the list is constructed, but before the ArrayAdapter is initialized.
//Sort the files alphabetically.Collections.sort(fileStringList);Collections.sort(pathStringList);fileListAdapter = new ArrayAdapter(this.getContext(), R.layout.open_file_entry, R.id.fileName, fileStringList); ListView lv = (ListView) this.findViewById(R.id.pcap_files_list);if(lv != null){lv.setAdapter(fileListAdapter);}
So far, this code is working as intended. I don't know if this is the most-optimal to do this, but that will be analyzed before I release this app to the market. For now, I'm still building out the basic infrastructure.
Very helpful! Thanks a lot :-)
ReplyDeleteThis is a nice article..
ReplyDeleteIts very easy to understand ..
And this article is using to learn something about it..
asp.net, c#, javascript
Thanks a lot..!
thank you bro it is very useful for me..
ReplyDelete