我正在开发一个项目,我正在尝试使用自定义数组适配器来获取设备上文件和目录的文件列表。
下面是我用来获取文件列表的代码:
public ArrayList<FileDirectoryDetails> getFileDirectoryListing()
{
ArrayList<FileDirectoryDetails> fileAndDirectories = new ArrayList<FileDirectoryDetails>();
final String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{
File[] files = Environment.getExternalStorageDirectory().listFiles();
for (int i = 0; i < files.length; i++)
{
String path = files[i].getName();
FileOrDirectory fileOrDirectory;
//FileDirectoryDetails fileDirectoryDetails = new FileDirectoryDetails();
//fileDirectoryDetails.path = files[i].getName();
if (files[i].isDirectory())
{
fileOrDirectory = FileOrDirectory.Directory;
//fileDirectoryDetails.fileOrDirectory = FileOrDirectory.Directory;
}
else
{
fileOrDirectory = FileOrDirectory.File;
//fileDirectoryDetails.fileOrDirectory = FileOrDirectory.File;
}
FileDirectoryDetails fileDirectoryDetails = new FileDirectoryDetails(path, fileOrDirectory);
fileAndDirectories.add(fileDirectoryDetails);
}
}
return fileAndDirectories;
}
下面是设置列表适配器的代码
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); listView = getListView();
ArrayList<FileDirectoryDetails> filesAndDirectories = getFileDirectoryListing();
fileDirectoryDetailsArrayAdapter = new
FileListingArrayAdapter<FileDirectoryDetails>(this, android.R.layout.simple_list_item_1, filesAndDirectories);
setListAdapter(fileDirectoryDetailsArrayAdapter);
}
下面是 FileListingArrayAdapter 的代码:
public class FileListingArrayAdapter<FileDirectoryDetails> extends ArrayAdapter<FileDirectoryDetails>
{
Context context;
ArrayList<FileDirectoryDetails> fileDirectoryDetails;
TextView textView = null;
public FileListingArrayAdapter(Context context, int resource,
int textViewResourceId, ArrayList<FileDirectoryDetails> fileListings) {
super(context, resource, textViewResourceId, fileListings);
this.context = context;
this.fileDirectoryDetails = fileListings;
}
public FileListingArrayAdapter(Context context, int resource, ArrayList<FileDirectoryDetails> fileListings)
{
super(context, resource, fileListings);
this.context = context;
this.fileDirectoryDetails = fileListings;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent)
{
View view = null;
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.file_explorer, null);
TextView textView = (TextView)view.findViewById(R.id.listTextView);
FileDirectoryDetails fileDetails = fileDirectoryDetails.get(pos);
//FileDirectoryDetails fileDirectoryDetails = new FileDirectoryDetails();
fileDetails.path;
return view;
}
下面是 FileDirectoryDetails 的类定义:
public class FileDirectoryDetails
{
public String path;
public FileOrDirectory fileOrDirectory;
public FileDirectoryDetails(String path, FileOrDirectory fileOrDirectory)
{
this.path = path;
this.fileOrDirectory = fileOrDirectory;
}
}
在自定义数组适配器中,我正在尝试以下操作:
FileDirectoryDetails fileDetails = fileDirectoryDetails.get(pos);
然后我试图访问 FileDirectoryDetails 类中的变量以提取路径名和枚举来说明它是文件还是目录。
但是,FileDirectoryDetails 中的变量不会显示在 Eclipse 的智能感知中。
我不明白为什么这两个变量无法访问。
感谢您的任何帮助,您可以提供。