0

我有以下代码:

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;
    }

}

我只需要以下几行的帮助:而不是使用手动输入数据的以下行:

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

我想完成以下任务:

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())));
}

"FaceBook"FileName()and替换并"8/2/2013"使用GetFileLM()循环,以便循环可以输入尽可能多的tc文件imageArry.add();

4

1 回答 1

0

尝试使用 Hasmap 而不是 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;
    }

}

代码未测试。可能需要进行细微更改。您将需要导入 HashMap 和 Map 类。

于 2013-08-05T22:13:32.037 回答