问题
I have the following code:
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;
}
The following line is giving an error:
File flEachFile = new File(folder.toString()+ "/" + FilesInFolder.get(position).toString());
The FiledInFolder.get(position).toString worked when the function was when the function only returned the filename only:
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;
}
The new function returns both the filename and lastmodified date. How do i achieve the same feature with the onClickListener so it works with the new function?
回答1:
Your GetFileData function is creating a HashMap not an ArrayList. You can not get an element from a HashMap by using its position. Instead individual Hashmap entries are obtained by passing its key (Here is key is filename itself). So you actually need a reference to the hashmap entry. You can use setTag() and getTag() functions of View class to achieve that.
few other alternatives you can try are
use findViewbyId
dataList.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { TextView clicked= (TextView) v.findViewById(R.id.id_of_textview_that_hold_filename); String current_file=clicked.getText().toString(); }Try to get filename from rowsArray using position. You may need to use an intermediate final variable to make rowsArray accessible inside listener
Make FilesData an ArrayList that holds a string array of size 2.
Inside GetFileData method, create a new string array of size two, store filename and modification date to position 0 & 1. Then add this string array to the array list. You need to change necessary variables from HashMap to ArrayList
回答2:
FilesInFolder is now a Hashmap, so you just want to get the filename at a given position (index), which is the first element of the map - the keys (the 2nd is the timestamp, or values of the map). So you could do something like this:
First convert the map keys to a list:
List<Value> filenames = new ArrayList<String>(FilesInFolder.keys());
then:
File flEachFile = new File(folder.toString()+ "/" + filenames.get(position).toString());
来源:https://stackoverflow.com/questions/18070227/reading-files-from-a-custom-function-is-fc-the-app