我有以下代码:
final RelativeLayout mFrame3 = (RelativeLayout) inflater.inflate( R.layout.ptrip, container, false );
folder = new File(Environment.getExternalStorageDirectory() + "/tc/");
FilesInFolder = GetFilesData(folder.getAbsolutePath());
FileSize = GetFileSize(folder.getAbsolutePath());
for (Map.Entry<String, String> current_file:FilesInFolder.entrySet()) {
rowsArray.add(new SetRows(R.drawable.ic_launcher, current_file.getKey().toString(), current_file.getValue().toString()));
}
adapter = new SetRowsCustomAdapter(getActivity(), R.layout.customlist, rowsArray);
ListView dataList = (ListView) mFrame3.findViewById(R.id.lvFiles);
dataList.setAdapter(adapter);
dataList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View
v, int position, long id) {
Toast.makeText(getActivity(), Integer.toString(position), 2000).show();
// Clicking on items
//Toast.makeText(getActivity(), FilesInFolder.get(position).toString() + FileLastModified.get(position).toString() + FileSize.get(position).toString(), 2000).show(); //display filename (0)
//Toast.makeText(getActivity(), folder.toString()+ "/" +FilesInFolder.get(position).toString(), Toast.LENGTH_LONG).show();
/*File flEachFile = new File(folder.toString()+ "/" + FilesInFolder.get(position).toString()); //read POSITION file
if (!flEachFile.exists()) { //if file does not exist close
throw new RuntimeException("File not found");
}
Log.e("Testing", "Starting to read");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(flEachFile));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line + "\n");
}
// custom dialog
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.displayfilecontents);
dialog.setTitle("Trip Name: " + FilesInFolder.get(position).toString().substring(0, FilesInFolder.get(position).toString().lastIndexOf(".")));
EditText text = (EditText) dialog.findViewById(R.id.etFileContents);
if (text != null) {
text.setFocusable(false);
text.setLongClickable(false);
text.setTextIsSelectable(false);
}
text.setText(builder);
Button btnCloseIt = (Button) dialog.findViewById(R.id.btnClose);
// if button is clicked, close the custom dialog
btnCloseIt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (reader != null) {
try {
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}*/
}
});
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;
}
以下行给出错误:
File flEachFile = new File(folder.toString()+ "/" + FilesInFolder.get(position).toString());
FiledInFolder.get(position).toString
函数只返回文件名时的工作:
public ArrayList<String> GetFilesData(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;
}
新函数返回文件名和最后修改日期。我如何使用 onClickListener 实现相同的功能,以便它与新功能一起使用?