0

我有以下代码:

        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 实现相同的功能,以便它与新功能一起使用?

4

2 回答 2

1

您的 GetFileData 函数正在创建 HashMap 而不是 ArrayList。您不能通过使用其位置从 HashMap 中获取元素。相反,单个 Hashmap 条目是通过传递其密钥获得的(这里的密钥是文件名本身)。因此,您实际上需要对 hashmap 条目的引用。您可以使用 View 类的 setTag() 和 getTag() 函数来实现这一点。

您可以尝试的其他几种选择是

  1. 使用 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();
    
    } 
    
  2. 尝试使用位置从 rowsArray 获取文件名。您可能需要使用中间最终变量来使 rowsArray 在侦听器中可访问

  3. 将 FilesData 设为 ArrayList,其中包含大小为 2 的字符串数组。

在 GetFileData 方法中,创建一个大小为 2 的新字符串数组,将文件名和修改日期存储到位置 0 和 1。然后将此字符串数组添加到数组列表中。您需要将必要的变量从 HashMap 更改为 ArrayList

于 2013-08-06T01:50:47.753 回答
0

FilesInFolder 现在是一个 Hashmap,因此您只想获取给定位置(索引)的文件名,这是地图的第一个元素 - 键(第二个是时间戳或地图的值)。所以你可以做这样的事情:

首先将映射键转换为列表:

List<Value> filenames = new ArrayList<String>(FilesInFolder.keys());

然后:

File flEachFile = new File(folder.toString()+ "/" + filenames.get(position).toString());
于 2013-08-06T02:35:44.557 回答