简体   繁体   中英

Get filename and file modified date to use for listview

I have the following code:

package com.example.customlistview;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.text.format.DateFormat;
import android.view.Menu;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
    ArrayList<Contact> imageArry = new ArrayList<Contact>();
    ContactImageAdapter adapter;
    File folder;
    int j = 0;
    ArrayList<String> FilesInFolder, FileLastModified;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        folder = new File(Environment.getExternalStorageDirectory() + "/tc/");
        FilesInFolder = GetFiles(folder.getAbsolutePath());
        //Toast.makeText(this, FilesInFolder.toString(), 2000).show();
        for (int i = 0; i < *WHATEVER NUMBER OF FILES*.length(); i++) {
            //
            //imageArry.add(new Contact(R.drawable.ic_launcher, FILENAME(FROM GETFILES()), FILEMODIFIED DATE(FROM GETFILELM())));
        }
        Toast.makeText(this, Integer.toString(j), 2000).show();
        // add image and text in arraylist
        imageArry.add(new Contact(R.drawable.ic_launcher, "FaceBook", "8/2/2013")); // take this out
        imageArry.add(new Contact(R.drawable.ic_launcher, "Google", "8/2/2013")); // take this out
        // add data in contact image adapter
        adapter = new ContactImageAdapter(this, R.layout.list, imageArry);
        ListView dataList = (ListView) findViewById(R.id.list);
        dataList.setAdapter(adapter);
    }

    public ArrayList<String> GetFiles(String DirectoryPath) {
        ArrayList<String> MyFiles = new ArrayList<String>();
        File f = new File(DirectoryPath);

        //f.mkdirs();
        File[] files = f.listFiles();
        if (files.length == 0)
            return null;
        else {
            for (int i=0; i<files.length; i++)  {
                if (files[i].getName().endsWith(".tol")) {
                    long lastTime = files[i].lastModified();
                    String fileName = files[i].getName().substring(0, files[i].getName().lastIndexOf("."));
                    MyFiles.add(fileName); //MyFiles.add(files[i].getName()); if extension is also needed
                }
            }
        }

        return MyFiles;
    }
    public ArrayList<String> GetFileLM(String DirectoryPath) {
        ArrayList<String> MyFiles = new ArrayList<String>();
        File f = new File(DirectoryPath);

        //f.mkdirs();
        File[] files = f.listFiles();
        if (files.length == 0)
            return null;
        else {
            for (int i=0; i<files.length; i++)  {
                if (files[i].getName().endsWith(".tol")) {
                    long lastTime = files[i].lastModified();
                    String dateString = DateFormat.format("MM/dd/yyyy", new Date(lastTime)).toString();
                    MyFiles.add(dateString);
                }
            }
        }

        return MyFiles;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

I just need some help for the following lines: Instead of using the following line which enters data manually:

imageArry.add(new Contact(R.drawable.ic_launcher, "FaceBook", "8/2/2013")); // take this out

I would like to accomplish the following:

for (int i = 0; i < *number of files in the folder*.length(); i++) {
    //
    //imageArry.add(new Contact(R.drawable.ic_launcher, FILENAME(FROM GETFILES()), FILE MODIFIED DATE(FROM GETFILELM())));
}

Replace "FaceBook" with the FileName() and "8/2/2013" with GetFileLM() and use a loop so the loop can enter as many files available from the tc using imageArry.add(); .

Try using a Hasmap instead of ArrayList.

 public class MainActivity extends Activity {
    ArrayList<Contact> imageArry = new ArrayList<Contact>();
    ContactImageAdapter adapter;
    File folder;
    int j = 0;
    Hashmap<String,String> FilesInFolder; //Filename:ModificationDate pairs

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        folder = new File(Environment.getExternalStorageDirectory() + "/tc/");
        FilesInFolder = GetFilesData(folder.getAbsolutePath());

        for (Map.Entry<String, String> current_file:FilesInFolder.entrySet()) {
            //


            imageArry.add(new Contact(R.drawable.ic_launcher, current_file.getKey(), current_file.getValue()));
        }

        adapter = new ContactImageAdapter(this, R.layout.list, imageArry);
        ListView dataList = (ListView) findViewById(R.id.list);
        dataList.setAdapter(adapter);
    }

    public HashMap<String,String> GetFilesData(String DirectoryPath) {
        HashMap<String,String> MyFiles = new HashMap<String,String>();
        File f = new File(DirectoryPath);


        File[] files = f.listFiles();
        if (files.length == 0)
            return null;
        else {
            for (int i=0; i<files.length; i++)  {
                if (files[i].getName().endsWith(".tol")) {
                    long lastTime = files[i].lastModified();
                    String fileName = files[i].getName().substring(0, files[i].getName().lastIndexOf("."));

                    String dateString = DateFormat.format("MM/dd/yyyy", new Date(lastTime)).toString();
                    MyFiles.put(fileName,dateString); //Add the new filename and its modification date to the Hasmap
                }
            }
        }

        return MyFiles;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Code not tested. Minor changes may be required. You will need to import HashMap & Map classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM