从自定义函数读取文件是FC的应用 [英] Reading files from a custom function is FC the app

查看:110
本文介绍了从自定义函数读取文件是FC的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的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());

FiledInFolder.get(位置)的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;
}

新的功能,同时返回文件名和上次更改日期。我如何实现与onClickListener相同的特征,因此新功能的工作原理?

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?

推荐答案

您GetFileData功能是创建一个HashMap不是一个ArrayList。你不能用它的位置从一个HashMap的一个元素。而不是单独的哈希映射条目由它传递密钥获得(这是关键的是文件名本身)。所以,你实际上需要HashMap中条目的引用。您可以使用setTag()和getTag()查看类的功能来实现这一点。

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


  1. 使用findViewbyId

  1. 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();

} 


  • 尝试使用位置从rowsArray获取文件名。您可能需要使用一个中间变量最终使听者内访问rowsArray

  • Try to get filename from rowsArray using position. You may need to use an intermediate final variable to make rowsArray accessible inside listener

    请FilesData持有大小为2的字符串数组一个ArrayList

    Make FilesData an ArrayList that holds a string array of size 2.

    里面GetFileData方法,创建一个大小为二,存储文件名和修改日期的一个新的字符串数组位置0&安培; 1,那么这个字符串数组添加到数组列表。您需要更改从HashMap的必要变量的ArrayList

    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

    这篇关于从自定义函数读取文件是FC的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆