Checking directory contents and file properties


We will start by checking the contents of the directory. The results will be displayed on the ListView component.

In the first place, I add such a component to my activity:

For this component, you will need to add a layout for individual items in the list (this is necessary for later stages), for now, I am creating an empty XML file in the layout directory.

Its content is as follows:
Nothing special. The file simply describes how a single item in a list looks like. Here it will be just an inscription, but we can use TableLayout here as well, and layout more complex structures (eg image and text).
It’s time for the code:
The most important elements are in lines 23-28. On line 23 of the list of String elements, I assign a list of files from the specified directory. The File class is a plain jav File, not an Android version. In Java, each directory is also a file, so this may be a bit strange record. The directory that I download is “.”, Ie the root directory of the system. As well, this may be any other directory, as a parameter for the File class constructor. On line 24, I am creating an ArrayList object. I will need to add an adapter (that is, an element that complements the contents of a ListView component) to a list of just that type. On line 26, I’m doing a “conversion” of the string that I’ve used so far into an ArrayList object. Line 27 is the initialization of the previously mentioned adapter. We have three parameters, the first is the context,
I changed the directory whose content I display on “/ sdcard”, ie the contents of the SD card.
We will now display the properties of the files and directories.
I’ve made some changes to the code. On line 27, I changed a call to the File class constructor to use a String variable named directory instead of the direct path. I did this because I’m still using the path to the file a bit and I do not want to duplicate the same code. From lines 29 to 37, iterates through elements of string lists containing filenames and directories. Depending on the properties of the file / directory to its name, I am storing different information. The remainder of the code remains unchanged. Line 31 is labeled R if the file / directory can be read. Line 32 is the addition to the letter W if we have the ability to write to the file. Lines 33-35 add a date to the file / directory name. I’m using a java.util.Date object here because the return value of the lastModified () method is int.
In the XML file list_group.xml describing the appearance of a single element of the list also made a small change (pure cosmetics). I reduced font size from 15 to 11 because more information would cause the line to wrap, so we have all the information about the file / directory in one line:)