SoFunction
Updated on 2025-04-14

Sample code for Android implementation files to be displayed in order of time

1. Project background and introduction

In many Android applications, local files need to be managed and displayed, such as file managers, log viewing tools, etc. The order of files is displayed by the last modification time, which allows users to intuitively understand the order of files creation or modification, so as to more conveniently find the latest or oldest files. This article will introduce how to obtain a list of files in a specified directory on the Android platform, sort it in order of the file modification time and then display it on the interface.

2. Working principle and key steps

2.1 File attribute acquisition

  • Get all files in the specified directory through Java IO or Android API.

  • For each file object, use()Method gets the last modification time (millisecond value).

2.2 Comparison of sorting algorithm and time

  • Using Java provided()or()Method sorts the file list.

  • Custom sorting comparator, according to()The return value of  sorts the file in ascending or descending order.

2.3 UI display and data binding

  • The sorted file list can be bound to a RecyclerView or ListView through Adapter.

  • Each list item displays file name, modification time and other information, allowing users to intuitively see the results of files sorted by time.

3. System design plan

3.1 Project requirements and function description

  • Get the file list: traverse all files in the specified directory and obtain the properties of each file (including modification time).

  • Sort display: The file list is sorted by the modification time, and it supports ascending or descending order.

  • Interface display: Use RecyclerView to display the sorted file list, the list items include the file name and modification date.

  • User interaction: Users can click on the file list item for further operations, or switch the sorting order (optional).

3.2 Overall architecture overview

The system is mainly divided into the following modules:

  • Data layer: Get the file list through the File API and sort it using Comparator.

  • Logical layer: Encapsulate file sorting and data conversion logic, converting it into a data format suitable for UI display.

  • UI layer: Display the file list through RecyclerView and Adapter, and use SimpleDateFormat to format the modification time.

4. Detailed code implementation

The following sample code is implemented in Java language, showing how to obtain files in a specified directory, sort by modification time, and display the file name and modification time through RecyclerView. You can integrate this code into your Android Studio project.

4.1 Get the file list

import ;
import ;
import ;
 
public class FileUtils {
    // Get the list of all files in the specified directory (not recursive subdirectories)    public static List<File> getFiles(String path) {
        List<File> fileList = new ArrayList<>();
        File dir = new File(path);
        if (() && ()) {
            File[] files = ();
            if(files != null) {
                for (File file : files) {
                    if (()) {
                        (file);
                    }
                }
            }
        }
        return fileList;
    }
}

4.2 Sort by modification time

import ;
import ;
import ;
import ;
 
public class FileSorter {
    // Ascending order (old files are in front and new files are in back)    public static void sortFilesByTime(List<File> files) {
        (files, new Comparator<File>() {
            @Override
            public int compare(File f1, File f2) {
                long time1 = ();
                long time2 = ();
                return (time1, time2);
            }
        });
    }
}

4.3 RecyclerView displays file list

Adapter Example

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
 
public class FileAdapter extends <> {
    private List<File> fileList;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", ());
 
    public FileAdapter(List<File> fileList) {
         = fileList;
    }
 
    @Override
    public FileViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = (()).inflate(.simple_list_item_2, parent, false);
        return new FileViewHolder(view);
    }
 
    @Override
    public void onBindViewHolder(FileViewHolder holder, int position) {
        File file = (position);
        holder.(());
        holder.("Modification time: " + (()));
    }
 
    @Override
    public int getItemCount() {
        return ();
    }
 
    class FileViewHolder extends  {
        TextView text1, text2;
        public FileViewHolder(View itemView) {
            super(itemView);
            text1 = (.text1);
            text2 = (.text2);
        }
    }
}

Use in Activity

import ;
import ;
import ;
import ;
import ;
import ;
 
public class FileListActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private FileAdapter fileAdapter;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView(.activity_file_list);
 
        recyclerView = findViewById();
        (new LinearLayoutManager(this));
 
        // Specify the directory path to display        String path = "/sdcard/YourFolder";
        List<File> files = (path);
        (files);
 
        fileAdapter = new FileAdapter(files);
        (fileAdapter);
    }
}

Correspondingactivity_file_list.xmlThe layout file can simply set a RecyclerView.

5. Code interpretation and test results

5.1 Code interpretation

  • File acquisition and sorting
    ()The function traverses all files in the specified directory;()The file list is sorted ascendingly according to the last modification time of the file through Comparator.

  • UI display
    existFileListActivityIn this article, use RecyclerView and custom Adapter to display the file name and formatted modification time.

  • Date formatting
    useSimpleDateFormatFormat the last modification time of the file, which is convenient for users to view intuitively.

5.2 Test results

  • After the application is started, all files in the specified directory will be displayed, arranged from morning to night according to the modification time.

  • Each item in RecyclerView displays the file name and corresponding modification time. Click the list item to expand to achieve further operations (such as opening a file).

  • During the test, make sure that the SD card read permission is configured (such as adding read storage permissions in it).

6. Project summary and experience

This project introduces in detail how to implement files in order of time on the Android platform. Main experiences include:

  • File processing
    Use the Java File API to obtain the file list and use thelastModified()Methods obtain time information and provide a basis for sorting.

  • Sorting algorithm
    use()and custom Comparator to implement sorting by modification time, which is simple and efficient.

  • UI display
    Use RecyclerView and Adapter to display the sorted file list, and format the time data through SimpleDateFormat to make the display more intuitive.

  • System extension
    This solution can be extended to support click-to-open files, switching between different sorting methods (such as file size, name, etc.), and multi-directory file management.

Overall, this project provides a complete reference solution for realizing file sorting display in Android applications, which is of great guiding significance for beginners to understand file processing, sorting algorithms and RecyclerView data binding.

The above is the detailed content of the sample code that displays Android implementation files in order of time. For more information about Android files in order of time, please pay attention to my other related articles!