[英]How can I tell if the selected index in a ListView is a file or directory?
I have an Activity with a ListView. 我有一个带ListView的Activity。 am using Apache FTP Commons to connect and populate the ListView with files and directories.
我使用Apache FTP Commons连接并使用文件和目录填充ListView。 How can I determine if the selected item is a file or directory?
如何确定所选项目是文件还是目录?
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();
}
}
The Toast returns the correct file name. Toast返回正确的文件名。
Here is the complete code using Backslash's suggestion for anyone who might find it useful. 以下是使用反斜杠建议的完整代码,供任何可能认为有用的人使用。
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();
}
}
}
When you use a FTPClient
by Apache Commons Net, if you list a directory the client will send you back an array of FTPFile
s. 当您使用Apache Commons Net的
FTPClient
,如果您列出一个目录,客户端将向您发送一个FTPFile
数组。 Each FTPFile
has a isDirectory()
and isFile()
method that can tell you if it is a directory or a file. 每个
FTPFile
都有一个isDirectory()
和isFile()
方法,可以告诉您它是目录还是文件。
Apache Commons FTPFile JavaDoc Apache Commons FTPFile JavaDoc
So you basically need to find which file has the selected name in your list (maybe serching in it using the getName()
function?) and then use one of the two methods provided in the FTPFile
class 所以你基本上需要在列表中找到哪个文件具有所选名称(可能使用
getName()
函数在其中进行搜索?)然后使用FTPFile
类中提供的两种方法之一
UPDATE UPDATE
Try this: 尝试这个:
Declare a HashMap instance variable 声明一个HashMap实例变量
//this will contain the file name and it's isDirectory() result
HashMap<String, Boolean> map = new HashMap<String, Boolean>();
Now, at the beginning of the ftpPrintFilesList(path)
method: 现在,在
ftpPrintFilesList(path)
方法的开头:
//resets the map, you are listing new FTPFiles, so older ones need to be removed
map.clear();
Then, in your for
loop, in ftpPrintFilesList(path)
add every FTPFile info to the map like this: 然后,在你的
for
循环中,在ftpPrintFilesList(path)
中将每个FTPFile信息添加到地图中,如下所示:
//Add file's name and isDirectory() value to the map
map.put(ftpFiles[i].getName(),new Boolean(ftpFiles[i].isDirectory()));
Finally, in the onItemClick
method, just use 最后,在
onItemClick
方法中,只需使用
boolean isDirectory = map.get(selectedFile).booleanValue();
It will return true
if the selected item is a directory, false
if it is a file 如果所选项目是目录,则返回
true
如果是文件,则返回false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.