0

我正在开发一个项目,我正在尝试使用自定义数组适配器来获取设备上文件和目录的文件列表。

下面是我用来获取文件列表的代码:

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;
    }

下面是 FileDirectoryDe​​tails 的类定义:

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);

然后我试图访问 FileDirectoryDe​​tails 类中的变量以提取路径名和枚举来说明它是文件还是目录。

但是,FileDirectoryDe​​tails 中的变量不会显示在 Eclipse 的智能感知中。

我不明白为什么这两个变量无法访问。

感谢您的任何帮助,您可以提供。

4

2 回答 2

1

而不是扩展 ArrayAdapter ,扩展 BaseAdapter 以编写您的自定义适配器类。这将使这变得更简单,并且会起作用。

于 2013-11-05T15:18:09.540 回答
0

您可以在 xml 中设置 ListView 然后

 ListView myListView = (ListView) findViewById(R.id.idOfListView);
 myListView.setAdapter(new FileListingArrayAdapter(this, R.layout.row_view, filesAndDirectories))

R.layout.row_view 将是您创建的 xml 页面,提供自定义列表行视图。在您的自定义适配器类中,您可以扩展行视图。将您的自定义适配器类定义为...

FileListingArrayAdapter extends ArrayAdapter<FileDirectoryDetails>

这是一个简单的教程,展示了自定义数组适配器的使用... http://stirfriedcode.blogspot.jp/2013/10/populating-listview-using-sqlite-android.html

于 2013-11-05T16:14:56.973 回答