在Android中初始化之前访问类对象

时间:2014-01-08 10:32:55

标签: java android

我有一个名为ItemsAdapter的类,其中我有三个参数,我在另一个名为ItemsActivity.While的类中实例化这个类,我需要提出所有三个参数,最后一个参数是我要通过的称为getImages()的方法,它位于同一类ItemsAdapter中。我只能通过使用ItemsAdapter对象来访问它,但这是不可行的。这就是我的尝试:

代码:

ItemsAdapter mAdapter = null;
ArrayList<String> image_items = mAdapter.getImages(); 
mAdapter = new ItemsAdapter(this,R.layout.item,image_items);

更新:

public ArrayList<String> getImages()
    {
        String path = android.os.Environment.getExternalStorageDirectory() + "/Pictures/SanPics2/";
        File mainfolder = new File(path);
        ArrayList<String> files = new ArrayList<String>();
        if(mainfolder.listFiles() == null)
        return null;
        for(File f : mainfolder.listFiles())
        {
            if(f.getName().contains(".jpg"))
            {
                files.add(f.getAbsolutePath());
            }
        }
        return files;
    }

这是我尝试访问的方法。 它会像预测的那样抛出NullPointerException。还有另一种方法可以访问ItemsAdapter类中的方法getImages()。抱歉,如果问题是愚蠢的(我知道它是),但我只是编程和东西的初学者。任何关于如何实现这一点的想法都将受到高度赞赏。提前致谢。

3 个答案:

答案 0 :(得分:1)

我建议您在Activtiy而不是适配器类中使用此代码。

public ArrayList<String> getImages()
    {
        String path = android.os.Environment.getExternalStorageDirectory() + "/Pictures/SanPics2/";
        File mainfolder = new File(path);
        ArrayList<String> files = new ArrayList<String>();
        if(mainfolder.listFiles() == null)
        return null;
        for(File f : mainfolder.listFiles())
        {
            if(f.getName().contains(".jpg"))
            {
                files.add(f.getAbsolutePath());
            }
        }
        return files;
    }

也使用blackbelt建议的适当构造函数

http://developer.android.com/reference/java/io/File.html

public File (String dirPath, String name)

Added in API level 1
Constructs a new File using the specified directory path and file name, placing a path separator between the two.

Parameters
dirPath the path to the directory where the file is stored.
name    the file's name.
Throws
NullPointerException    if name == null.

然后

ArrayList<String> image_items = getImages(); 

然后

ItemsAdapter mAdapter = null;
mAdapter = new ItemsAdapter(this,R.layout.item,image_items); 
// pass the list to the constructor of adapter class and use it there

答案 1 :(得分:1)

将方法设为静态并使用类名称调用

public static ArrayList<String> getImages()
    {
        String path = android.os.Environment.getExternalStorageDirectory() + "/Pictures/SanPics2/";
        File mainfolder = new File(path);
        ArrayList<String> files = new ArrayList<String>();
        if(mainfolder.listFiles() == null)
        return null;
        for(File f : mainfolder.listFiles())
        {
            if(f.getName().contains(".jpg"))
            {
                files.add(f.getAbsolutePath());
            }
        }
        return files;
    }

现在称为

ArrayList<String> image_items = ItemsAdapter.getImages(); 

更好的用法是将此方法移动到相同的Activity类ItemsActivity中(如果没有其他类调用此方法)。因此,将此代码放入ItemsActivity而不制作静态,例如

private ArrayList<String> getImages() {....} // Same as you posted into question, but public changed to private.

并将此方法调用为与

相同的活动ItemsActivity
mAdapter = new ItemsAdapter(this,R.layout.item, getImages());

答案 2 :(得分:0)

为什么不更改构造函数并为图像添加setMethod,如果必须从适配器绘制图像?喜欢这个?

ItemsAdapter mAdapter = new ItemsAdapter(this,R.layout.item);
ArrayList<String> image_items = mAdapter.getImages(); 
mAdapter.setImages(image_items);

或者只是在实例化适配器时执行此操作?更为优雅的方法

public ItemsAdapter(Context context, int layout) {
   //some super call
   //some work
   this.images = getImages();
}