0

I have a class named ItemsAdapter where I have three arguments and I am instantiating this class in another class named ItemsActivity.While doing so, I need to put up all the three arguments and the last argument is where I am gonna pass a method known as getImages() which is in the same class ItemsAdapter. I can access this only by using the ItemsAdapter object but it's not feasible. This is what I have tried :

CODE :

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

UPDATE :

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

This is the method which I am trying to access. It throws NullPointerException as predicted. Is there a another way around this where I can access the method getImages() in ItemsAdapter class. Apologies if the question is dumb(i know it is) but I am just a beginner to programming and stuffs. Any idea on how to achieve this would be highly appreciated. Thanks in advance.

11
  • why your adapter holds that getImages call? What is its purpose? Commented Jan 8, 2014 at 10:33
  • where is getImages?. What does it return? Commented Jan 8, 2014 at 10:33
  • @Raghunandan getImages() returns the list of images present at a certain location in SDCard and it is in the ItemsAdapter class. Commented Jan 8, 2014 at 10:35
  • Adapter should be initialize before use. its not initialized Commented Jan 8, 2014 at 10:35
  • 1
    ImageAdapter can determine the images itself, no need to put them into the constructor. Commented Jan 8, 2014 at 10:38

3 Answers 3

1

Make method as static and call it with class name

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

And now call as

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

Better use is, to move this method into same Activity class ItemsActivity (if there is no other classes call this method). So put this code into ItemsActivity without making static, like

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

And call this method into same Activity ItemsActivity as

mAdapter = new ItemsAdapter(this,R.layout.item, getImages());
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. A simple and neat answer. Deserves a vote up and tick.
1

I would suggest you have this code in Activtiy rather than adapter class.

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

Also use appropriate constructor as suggested by 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.

Then

ArrayList<String> image_items = getImages(); 

Then

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

10 Comments

he should use the constructor with two parameters of File instead. In turns that constructor makes use of File.Separator
@Raghunandan That did not work. It would throw NullPointerException.
@Raghunandan Thanks for your effort but I have accepted an answer and thats the most simple answer to it.
@San good for you. But this method also works provided you do it right.
@Raghunandan I am just curious and wanna know. I have placed the getImages() in the same activity where I am instantiating ItemsAdapter then why would I require the ItemsAdapter object to access that method. I have tried the following and it works like a charm. ArrayList<String> image_items = getImages() is what I have done. I dont understand why you have given as ArrayList<String> image_items = mAdapter.getImages() which would simply wouldn't work if the getImages() is in same class. I request you to edit your answer.
|
0

Why not Change your constructor and add a setMethod for the images if you have to draw the images from adapter? Like this?

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

or just do that work while instantiating the adapter? Much more ellegant approach

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

2 Comments

So the getter puts it into the setter? Looks weird. Just use the getter inside the adapter should do here.
yes it does, i would just do all that in the adapter itself. just working with what i got :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.