0

I want to store my API result in a Array List, need to store, ID and ImageURL. I am able to store the data using my class ImgModel. But I can't figureout how to access it later on.

public class ImgModel{

private String url, id;

    public ImgModel(String id, String url) {
        this.id = id;
        this.url = url;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getId() {
        return id;
    }

    public void setId(String photoId) {
        this.id = photoId;
    }

}

in MainActivity I call the API

public class MainActivity ....{
...
List<ImgModel> photosList = new ArrayList<ImgModel>();
....

//>>in the result API... after parse the json

String id   = imgOgj.getString("id");
String url  = imgOgj.getString("url");

ImgModelp p = new ImgModel(id, url);
photosList.add(p); //THIS WORKS

}

This Part I don't know how to implement - pls help

Now in the ImagePreview Activity I want to access these images and Id to display in Image view.

    public class ImagePreviewActivity ....{

    //List<ImgModel> mProcessedImg= new ArrayList<ImgModel>(); //If I do this, that means I am creating a new list, and not accessing the store data right ?
    ProcessedImg mProcessedImg;
    ImageView mImageView;


onCreate{

    ....
    mProcessedImg.size(); //Get the size .i.e how make images url 

    mImageView.setImage(mProcessedImg.getUrl(0);//sample how can I get the url of position 0 ?


    } 

    }
Kushal
  • 8,100
  • 9
  • 63
  • 82
Thiago
  • 12,778
  • 14
  • 93
  • 110

3 Answers3

1

The photosList variable that you have declared in MainActivity is a local variable, which means that its scope is limited to only the code block in which it has been declared. This is the reason that you cannot access the data you have stored in that variable elsewhere in your code.

In order to use and access that same variable again outside of the code block in which it was declared you could instead use an instance variable of the MainActivity class, by amending your class declaration as follows:

public class MainActivity extends Activity {
   List<ImgModel> mPhotosList;
   ...
   // Override the OnCreate method of Activity
   @Override
   public void onCreate(Bundle savedInstanceState) {
       // Create the mPhotosList instance variable
       mPhotosList = new ArrayList<ImgModel>;
       ...
   }

   // other methods where you call the API and store the data in mPhotosList
   ...

}

These pages may help to explain the differences between the types of variables that you can use in Java:

what is the difference between local and instance variables in Java

http://www.cs.umd.edu/~clin/MoreJava/Objects/local.html

In terms of the next part of your problem, to access the mPhotosList member variable from another Activity, the following post may help:

Passing a Bundle on startActivity()?

Community
  • 1
  • 1
Jadent
  • 974
  • 8
  • 14
0

If you neeed to share a list between lots of activities,putting it into MyApp's instance maybe a solution.

yuan tian
  • 41
  • 8
0

Create constructor inside ImagePreviewActivity.class which allows one List parameter.

    public class ImagePreviewActivity ....{

    List<ImgModel> imgList; 


    ImageView mImageView;

    public ImagePreviewActivity(List<ImgModel> imageList){
     this.imgList = imageList;
    }


onCreate{


    mImageView.setImage(imageList.get(0).getUrl();


    } 

    }

Creating a object of ImagePreviewActivity.class

public class MainActivity ....{
...
List<ImgModel> photosList = new ArrayList<ImgModel>();
....

String id   = imgOgj.getString("id");
String url  = imgOgj.getString("url");

ImgModelp p = new ImgModel(id, url);
photosList.add(p); 
//Craeate Object of ImagePreviewActivity
ImagePreviewActivity ipa = new ImagePreviewActivity(photosList);

}
Anushka
  • 31
  • 7