我有以下代码:
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();
。