From f43caa25823e6f5ce5c8a41b7dda4d76fee2234c Mon Sep 17 00:00:00 2001 From: Aario Shahbany Date: Mon, 27 Mar 2017 19:54:26 +0200 Subject: [PATCH] Added file modified timestamp to the list --- .../main/java/info/aario/snotepad/Filer.java | 9 +++++++ .../info/aario/snotepad/ListFragment.java | 25 ++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/info/aario/snotepad/Filer.java b/app/src/main/java/info/aario/snotepad/Filer.java index 9133ed3..b2b767a 100644 --- a/app/src/main/java/info/aario/snotepad/Filer.java +++ b/app/src/main/java/info/aario/snotepad/Filer.java @@ -9,6 +9,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.Date; /** * Created by aario on 3/16/17. @@ -99,4 +100,12 @@ public String getStringFromFile(String filePath) { } } + public String getModifiedTimestamp(String filePath) { + File fl = new File(filePath); + String ret = ""; + if (fl.exists()) + ret = new Date(fl.lastModified()).toString(); + return ret; + } + } diff --git a/app/src/main/java/info/aario/snotepad/ListFragment.java b/app/src/main/java/info/aario/snotepad/ListFragment.java index a072ba6..b051e0d 100644 --- a/app/src/main/java/info/aario/snotepad/ListFragment.java +++ b/app/src/main/java/info/aario/snotepad/ListFragment.java @@ -13,11 +13,13 @@ import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SearchView; +import android.widget.SimpleAdapter; import java.io.File; import java.io.FileFilter; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -30,7 +32,7 @@ public class ListFragment extends Fragment { private final String extension = ".txt"; ListView lvFiles; SearchView svSearch; - ArrayList filePathList = new ArrayList(); + ArrayList fileNameList = new ArrayList(); Map contentsCache = new HashMap(); @@ -63,12 +65,23 @@ public boolean accept(File file) { } private void populateFilesList() { - ArrayAdapter arrayAdapter = new ArrayAdapter(activity, android.R.layout.simple_list_item_1, filePathList); - lvFiles.setAdapter(arrayAdapter); + List> data = new ArrayList>(); + for (String fileName : fileNameList) { + Map item = new HashMap(2); + item.put("title", fileName); + item.put("date", activity.filer.getModifiedTimestamp(path + "/" + fileName)); + data.add(item); + } + SimpleAdapter adapter = new SimpleAdapter(activity, data, + android.R.layout.simple_list_item_2, + new String[]{"title", "date"}, + new int[]{android.R.id.text1, + android.R.id.text2}); + lvFiles.setAdapter(adapter); } private void search(String text) { - filePathList = searchFiles(text); + fileNameList = searchFiles(text); populateFilesList(); } @@ -98,7 +111,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, lvFiles.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { - activity.editFile(path + "/" + filePathList.get(arg2)); + activity.editFile(path + "/" + fileNameList.get(arg2)); } }); registerForContextMenu(lvFiles); @@ -139,7 +152,7 @@ public boolean onContextItemSelected(final MenuItem item) { //noinspection SimplifiableIfStatement if (id == R.id.action_delete) { - String filename = filePathList.get(itemInfo.position); + String filename = fileNameList.get(itemInfo.position); if (activity.filer.delete(path + "/" + filename)) { search(svSearch.getQuery().toString()); activity.makeSnackBar("File " + filename + " successfully deleted.");