2013-04-18 69 views
0

我正在使用Apache Commons的ftp庫,並用System.out.println()獲得了一個文件列表,但是如何將返回的文件名加載到ListView?我已經搜索,但找不到任何示例顯示這一點。有沒有人有一個在ListView中顯示FTP文件的例子?有沒有人有使用FtpClient.listFiles在ListView中顯示文件的例子?

+0

請參閱本 [S/O問題] [1] [1]:http://stackoverflow.com/questions/2576647/issue-with-org-apache- commons-net-ftp-ftpclient-listfiles 包含一個可能對您有幫助的示例。 –

+0

沒有,沒有任何UI的東西。 – RapsFan1981

+0

我其實已經想通了。完成文件類型圖標。看我的解決方案 – RapsFan1981

回答

0
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(); 
           } 
          } 
         } 
        } 

       }); 
相關問題