我有以下代码:
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()
,将"8/2/2013"
替换为GetFileLM()
并使用循环,以便循环可以使用{tc
输入尽可能多的文件{1}}。
答案 0 :(得分:0)
尝试使用Hashmap而不是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和;地图类。