【问题标题】:Does anyone have an example of using FtpClient.listFiles to show files in a ListView?有没有人有使用 FtpClient.listFiles 在 ListView 中显示文件的示例?
【发布时间】:2013-04-18 15:41:19
【问题描述】:

我正在使用 Apache Commons ftp 库并使用 System.out.println() 获得文件列表,但是如何将返回的文件名加载到 ListView?我已经搜索过,但找不到任何显示这一点的示例。有人有在 ListView 中显示 FTP 文件的示例吗?

【问题讨论】:

  • 请参阅此 [S/O 问题][1] [1]:stackoverflow.com/questions/2576647/… 包含一个可以帮助您的示例。
  • 不,没有任何 UI 的东西。
  • 我真的想通了。完整的文件类型图标。查看我的解决方案

标签: java android apache listview ftp


【解决方案1】:
liveToggle
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        if (isChecked) {
                            directoryEntries.clear();

                            FTPClient client = new FTPClient();

                            try {
                                client.connect(address);
                                client.login(username, password);

                                //
                                // Obtain a list of filenames in the current
                                // working
                                // directory. When no file found an empty array
                                // will
                                // be returned.
                                //
                                String[] names = client.listNames();
                                Drawable currentIcon = null;
                                for (String name : names) {
                                    File tmpFile = new File(name);
                                    String fileName = tmpFile.getName();
                                    if (checkEndsWithInStringArray(
                                            fileName,
                                            getResources().getStringArray(
                                                    R.array.fileEndingJs))) {
                                        currentIcon = getResources()
                                                .getDrawable(R.drawable.mimejs);
                                    } else if (checkEndsWithInStringArray(
                                            fileName,
                                            getResources().getStringArray(
                                                    R.array.fileEndingHTML))) {
                                        currentIcon = getResources()
                                                .getDrawable(
                                                        R.drawable.mimehtml);
                                    } else if (checkEndsWithInStringArray(
                                            fileName,
                                            getResources().getStringArray(
                                                    R.array.fileEndingCSS))) {
                                        currentIcon = getResources()
                                                .getDrawable(R.drawable.mimecss);
                                    } else if (checkEndsWithInStringArray(
                                            fileName,
                                            getResources().getStringArray(
                                                    R.array.fileEndingXML))) {
                                        currentIcon = getResources()
                                                .getDrawable(R.drawable.mimexml);
                                    } else if (checkEndsWithInStringArray(
                                            fileName,
                                            getResources().getStringArray(
                                                    R.array.fileEndingPhp))) {
                                        currentIcon = getResources()
                                                .getDrawable(R.drawable.mimephp);
                                    } else {
                                        currentIcon = getResources()
                                                .getDrawable(R.drawable.mimetxt);
                                    }
                                    directoryEntries.add(new IconifiedText(
                                            tmpFile.getPath(), currentIcon));
                                    System.out.println("Name = " + name);
                                }
                                FTPFile[] ftpFiles = client.listFiles();
                                for (FTPFile ftpFile : ftpFiles) {
                                    //
                                    // Check if FTPFile is a regular file
                                    //
                                    if (ftpFile.getType() == FTPFile.FILE_TYPE) {
                                        System.out.println("FTPFile: "
                                                + ftpFile.getName()
                                                + "; "
                                                + FileUtils
                                                        .byteCountToDisplaySize(ftpFile
                                                                .getSize()));
                                    }
                                }
                                client.logout();
                            } catch (IOException e) {
                                e.printStackTrace();
                            } finally {
                                try {
                                    client.disconnect();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }

                });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 2022-11-09
    • 2022-11-15
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    相关资源
    最近更新 更多