【发布时间】:2014-05-21 15:48:09
【问题描述】:
我看过其他帖子,但似乎无法在我的帖子中很好地表达出来。有人可以帮我解决这个问题。我可以查看文件并打开它们,但需要它们从最近创建到最旧列出。下面是我的代码。
private List<String> fileList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String root = Environment.getExternalStoragePublicDirectory("/MyAppFolder").
getAbsolutePath();
// ListDir(root);
pdf = new File(root);
ListDir(pdf);
}
void ListDir(File f) {
File[] files = f.listFiles();
fileList.clear();
for (File file : files) {
fileList.add(file.getName());
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
//selection.setText(fileList.indexOf(simple_list_item_1));
OpenPdf(fileList.get(position).toString());
}
public void OpenPdf(String path)
{
File file = new File(path);
if (file.exists()) {
Uri p = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(p, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e){
}
}
}
}
【问题讨论】:
-
此副本是相关问题列表中的第一个。请进行一些研究。
标签: java android file date comparator