使用定制阵列适配器

时间:2013-11-05 15:11:39

标签: android android-arrayadapter

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

以下是我用于获取文件列表的代码:

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中的intellisense中。

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

感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:1)

扩展BaseAdapter以编写自定义Adapter类,而不是扩展ArrayAdapter。这将使这更简单,它将工作。

答案 1 :(得分: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