Reputation: 9225
I have the following Method:
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
final RelativeLayout mFrame3 = (RelativeLayout) inflater.inflate( R.layout.ptrip, container, false );
File folder = new File(Environment.getExternalStorageDirectory() + "/tc/");
ListView lv;
ArrayList<String> FilesInFolder = GetFiles(folder.getAbsolutePath());
Toast.makeText(getActivity(), FilesInFolder.get(0).toString(), 2000).show(); //display filename (0)
////////////////////////
///////////////////////
//would this work:
//for (int i = 0; i <= FilesInFolder.length(); i++) {
//long lastTime = FilesInFolder[0].lastModified();
//String dateString = DateFormat.format("MM/dd/yyyy", new Date(lastTime)).toString(); //last modified date?
//String sizeString = getReadableSize(FolderInFiles[i].length()); //file size?
//}
//////////////////////
//////////////////////
lv = (ListView) mFrame3.findViewById(R.id.lvFiles);
lv.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, FilesInFolder));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Clicking on items
//Toast.makeText(getActivity(), "Item " + (position + 1) + ": ID" + v, Toast.LENGTH_SHORT).show();
}
});
return mFrame3;
}
My GetFolder function is:
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 dateString = DateFormat.format("MM/dd/yyyy", new Date(lastTime)).toString();
String sizeString = getReadableSize(files[i].length());
//String fileName = files[i].getName().substring(0, files[i].getName().lastIndexOf("."));
MyFiles.add(files[i].getName());
//MyFiles.add("Trip Name: " + fileName + "\nTrip Taken On: " + dateString + "\n" + sizeString);
}
}
}
return MyFiles;
}
How do I get the fileSize
and lastModifiedDate
inside the onCreateMethod
? I commented out the fileSize
and lastModifiedDate
inside the GetFolder
method because I am trying to get the filename only so I can read the file inside onCreate
method to display the ASCII contents of it in a MultiLine EditText.
Would this work in onCreate
as added above:
//display filename (0)
////////////////////////
///////////////////////
//would this work:
//for (int i = 0; i <= FilesInFolder.length(); i++) {
//long lastTime = FilesInFolder[0].lastModified();
//String dateString = DateFormat.format("MM/dd/yyyy", new Date(lastTime)).toString(); //last modified date?
//String sizeString = getReadableSize(FolderInFiles[i].length()); //file size?
//}
//////////////////////
//////////////////////
Upvotes: 1
Views: 501
Reputation: 12998
You have three options
If you do not want to change GetFiles
: In onCreate
, create a File
instance from the file names, and call the methods there. This is the only option, if you cannot change GetFiles
for whatever reason.
Create a class MyFile
which has three fields (name
, fileSize
and lastModifiedDate
), and return a List<MyFile>
from GetFiles
. Generally speaking this option is useful, if you only want to return a limited subset of properties or read-only views of an instance (without setters for example).
Return a List<File>
from GetFiles
The last option is simple and idiomatic.
Upvotes: 1