0

ListView を持つアクティビティがあります。Apache FTP Commons を使用して、ListView に接続し、ファイルとディレクトリを設定しています。選択したアイテムがファイルかディレクトリかを判断するにはどうすればよいですか?

public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.ftp);

        editAddress = (EditText) findViewById(R.id.editAddress);
        editUser = (EditText) findViewById(R.id.editUsername);
        editPassword = (EditText) findViewById(R.id.editPassword);
        remote = (ListView) findViewById(R.id.localList);
        itla.setListItems(directoryEntries);
        remote.setAdapter(itla);

        remote.setOnItemClickListener(this);
        connect = (ToggleButton) findViewById(R.id.connectButton);
        connect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                address = editAddress.getText().toString();
                user = editUser.getText().toString();
                pass = editPassword.getText().toString();
                ftpConnect(address, user,pass, 21);
            }
        });

    }

    public boolean ftpConnect(String host, String username, String password,
            int port) {
        try {
            mFTPClient = new FTPClient();
            // connecting to the host
            mFTPClient.connect(host, port);

            // now check the reply code, if positive mean connection success
            if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                // login using username & password
                boolean status = mFTPClient.login(username, password);

                /*
                 * Set File Transfer Mode
                 * 
                 * To avoid corruption issue you must specified a correct
                 * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
                 * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE for
                 * transferring text, image, and compressed files.
                 */
                mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
                mFTPClient.enterLocalPassiveMode();

                ftpPrintFilesList(ftpGetCurrentWorkingDirectory());
                return status;
            }
        } catch (Exception e) {
            // Log.d(TAG, "Error: could not connect to host " + host );
        }

        return false;
    }

    public boolean ftpDisconnect() {
        try {
            mFTPClient.logout();
            mFTPClient.disconnect();
            return true;
        } catch (Exception e) {
            // Log.d(TAG,
            // "Error occurred while disconnecting from ftp server.");
        }

        return false;
    }

    public String ftpGetCurrentWorkingDirectory() {
        try {
            String workingDir = mFTPClient.printWorkingDirectory();
            return workingDir;
        } catch (Exception e) {
            // Log.d(TAG, "Error: could not get current working directory.");
        }

        return null;
    }

    public boolean ftpChangeDirectory(String directory_path) {
        try {
            return mFTPClient.changeWorkingDirectory(directory_path);
        } catch (Exception e) {
            // Log.d(TAG, "Error: could not change directory to " +
            // directory_path);
        }

        return false;
    }

    public void ftpPrintFilesList(String dir_path) {
        try {
            FTPFile[] ftpFiles = mFTPClient.listFiles(dir_path);
            int length = ftpFiles.length;

            for (int i = 0; i < length; i++) {
                File name = new File(ftpFiles[i].getName());
                boolean isFile = ftpFiles[i].isFile();
                boolean isDirectory = ftpFiles[i].isDirectory();
                if (isDirectory) {
                    Drawable currentIcon = getResources().getDrawable(
                            R.drawable.folder);
                    directoryEntries.add(new IconifiedText(name, currentIcon));

                    // Log.i(TAG, "File : " + name);
                } else if (isFile) {
                    Drawable currentIcon = getResources().getDrawable(
                            R.drawable.mimetxt);
                    directoryEntries.add(new IconifiedText(name, currentIcon));
                    // Log.i(TAG, "Directory : " + name);
                }
                Collections.sort(this.directoryEntries);
                itla.setListItems(directoryEntries);
                remote.setAdapter(itla);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapter, View view, int position,
            long id) {
        String selectedFile = directoryEntries.get(position).getText();
        try {
            ftpPrintFilesList(selectedFile);
            itla.notifyDataSetChanged();
        } catch (NullPointerException e) {

        }
        Toast.makeText(FTPConnector.this, selectedFile, Toast.LENGTH_LONG)
                .show();

    }
}

Toast は正しいファイル名を返します。

バックスラッシュが役立つと思われる方向けの提案を使用した完全なコードを次に示します。

public class FTPConnector extends Activity implements OnItemClickListener {

    public ListView remote;
    public ArrayAdapter<String> adapter;
    public FTPClient mFTPClient = null;
    public ToggleButton connect;
    public EditText editAddress;
    private String address;
    public String currentDirectory = "/";
    public EditText editUser;
    private String user;

    public EditText editPassword;
    private String pass;
    public File[] fileList;
    public HashMap<String, Boolean> map = new HashMap<String, Boolean>();

    private List<IconifiedText> directoryEntries = new ArrayList<IconifiedText>();
    private IconifiedTextListAdapter itla = new IconifiedTextListAdapter(this);
    public String[] files;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.ftp);

        editAddress = (EditText) findViewById(R.id.editAddress);
        editUser = (EditText) findViewById(R.id.editUsername);
        editPassword = (EditText) findViewById(R.id.editPassword);
        remote = (ListView) findViewById(R.id.remoteList);
        itla.setListItems(directoryEntries);
        remote.setAdapter(itla);

        remote.setOnItemClickListener(this);
        connect = (ToggleButton) findViewById(R.id.connectButton);
        connect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if (isChecked) {
                    address = editAddress.getText().toString();
                    user = editUser.getText().toString();
                    pass = editPassword.getText().toString();
                    ftpConnect(address, user,
                            pass, 21);
                } else {
                    ftpDisconnect();
                    directoryEntries.clear();
                    itla.notifyDataSetChanged();
                }
            }
        });

    }

    public boolean ftpConnect(String host, String username, String password,
            int port) {
        try {
            mFTPClient = new FTPClient();
            // connecting to the host
            mFTPClient.connect(host, port);

            // now check the reply code, if positive mean connection success
            if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                // login using username & password
                boolean status = mFTPClient.login(username, password);

                /*
                 * Set File Transfer Mode
                 * 
                 * To avoid corruption issue you must specified a correct
                 * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
                 * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE for
                 * transferring text, image, and compressed files.
                 */
                mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
                mFTPClient.enterLocalPassiveMode();

                ftpPrintFilesList(ftpGetCurrentWorkingDirectory());
                return status;
            }
        } catch (Exception e) {
            // Log.d(TAG, "Error: could not connect to host " + host );
        }

        return false;
    }

    public boolean ftpDisconnect() {
        try {
            mFTPClient.logout();
            mFTPClient.disconnect();
            return true;
        } catch (Exception e) {
            // Log.d(TAG,
            // "Error occurred while disconnecting from ftp server.");
        }

        return false;
    }

    public String ftpGetCurrentWorkingDirectory() {
        try {
            String workingDir = mFTPClient.printWorkingDirectory();
            return workingDir;
        } catch (Exception e) {
            // Log.d(TAG, "Error: could not get current working directory.");
        }

        return null;
    }

    public boolean ftpChangeDirectory(String directory_path) {
        try {
            return mFTPClient.changeWorkingDirectory(directory_path);
        } catch (Exception e) {
            // Log.d(TAG, "Error: could not change directory to " +
            // directory_path);
        }

        return false;
    }

    public void ftpPrintFilesList(String dir_path) {
        map.clear();
        try {
            FTPFile[] ftpFiles = mFTPClient.listFiles(dir_path);
            int length = ftpFiles.length;

            for (int i = 0; i < length; i++) {
                File name = new File(ftpFiles[i].getName());
                boolean isFile = ftpFiles[i].isFile();
                boolean isDirectory = ftpFiles[i].isDirectory();
                if (isDirectory) {
                    Drawable currentIcon = getResources().getDrawable(
                            R.drawable.folder);
                    directoryEntries.add(new IconifiedText(name, currentIcon));
                    map.put(ftpFiles[i].getName(),
                            Boolean.valueOf(ftpFiles[i].isDirectory()));
                    // Log.i(TAG, "File : " + name);
                } else if (isFile) {
                    Drawable currentIcon = getResources().getDrawable(
                            R.drawable.mimetxt);
                    directoryEntries.add(new IconifiedText(name, currentIcon));
                    map.put(ftpFiles[i].getName(),
                            Boolean.valueOf(ftpFiles[i].isFile()));
                    // Log.i(TAG, "Directory : " + name);
                }
                Collections.sort(this.directoryEntries);
                itla.setListItems(directoryEntries);
                remote.setAdapter(itla);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapter, View view, int position,
            long id) {

        String selectedFile = directoryEntries.get(position).getText();
        boolean isDirectory = map.get(selectedFile).booleanValue();
        if (isDirectory) {
            directoryEntries.clear();
            itla.notifyDataSetChanged();
            ftpPrintFilesList(selectedFile);
        } else if (!isDirectory) {
            Toast.makeText(FTPConnector.this, selectedFile, Toast.LENGTH_LONG)
                    .show();
        }

    }
}
4

1 に答える 1

2

FTPClientApache Commons Net でを使用する場合、ディレクトリをリストすると、クライアントからFTPFileの配列が返されます。それぞれに、それがディレクトリかファイルかをFTPFile示すisDirectory()andメソッドがあります。isFile()

Apache Commons FTPFile JavaDoc

したがって、基本的には、リストで選択した名前を持つファイルを見つけて (おそらく関数を使用して検索しますか?)、クラスgetName()で提供される 2 つのメソッドのいずれかを使用する必要があります。FTPFile

アップデート

これを試して:

HashMap インスタンス変数を宣言する

//this will contain the file name and it's isDirectory() result
HashMap<String, Boolean> map = new HashMap<String, Boolean>();

さて、ftpPrintFilesList(path)メソッドの冒頭で:

//resets the map, you are listing new FTPFiles, so older ones need to be removed
map.clear(); 

次に、forループ内で、ftpPrintFilesList(path)すべての FTPFile 情報を次のようにマップに追加します。

//Add file's name and isDirectory() value to the map
map.put(ftpFiles[i].getName(),new Boolean(ftpFiles[i].isDirectory()));

最後に、onItemClickメソッドで使用するだけです

boolean isDirectory = map.get(selectedFile).booleanValue();

true選択したアイテムがディレクトリのfalse場合、ファイルの場合は返されます

于 2013-04-27T21:42:07.287 に答える