0

i will like to pass pictureActivity.java array to another class imageAdapter.java. I do get the imgPath result on toast, but i have no idea how to move the string to another class.

PictureActivity.java

ArrayList<HashMap<String, String>> imgList = new ArrayList<HashMap<String, String>>();

    try {
        imageLink = json.getJSONArray(TAG_IMG);

        for(int i = 0; i < imageLink.length(); i++){
            JSONObject c = imageLink.getJSONObject(i);
            String imgPath = c.getString(TAG_PATH);

            HashMap<String, String> map = new HashMap<String, String>();
            map.put(TAG_PATH, imgPath);

            imgList.add(map);
            Toast.makeText(this, map.get(TAG_PATH), Toast.LENGTH_LONG).show();
        }
    }catch (JSONException e) {
        e.printStackTrace();
    }
    ((Gallery) findViewById(R.id.gallery))
            .setAdapter(new ImageAdapter(this));

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {

    private Context myContext;
    public String[] myRemoteImages = {
            //How to add imagepath here??
    };

I wanted to put the array string on myRemoteImages.

1 Answer 1

1

you can pass your array adapter while you initialize the adapter class object:

((Gallery) findViewById(R.id.gallery))
            .setAdapter(new ImageAdapter(this, imgList));

and in your adapter class create a constructor like this:

public class ImageAdapter extends BaseAdapter {
  ArrayList<HashMap<String, String>> data = null;
  public String[] myRemoteImages;
  private Context myContext;

  public ImageAdapter(Context context, ArrayList<HashMap<String, String>> data){
   this.mContext = context;
   this.data = data;

   myRemoteImages = new String[this.data.size()];
   for(int i=0; i<this.data.size(); i++){
     HashMap<String, String> map = this.data.get(i);
     myRemoteImages[i] = map.get(TAG_PATH).toString();
   }

  }
}

after getting the ArrayList in your adapter class, you may fetch whatever value you want out from it.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Waqas, i am still relatively new to Java and Android. How do i play with "data", to get the value? Can u show me the code? I tried few way but i still can't figure it out. e.g. public String[] myRemoteImages = {data.toString()}; Thanks
i've updated the answer. now it shows how you can create an array of paths
Thanks very much Waqas!! Btw imgList.size() & imgList.get(i) the imgList cannot be resolved. I am using data.size() & data.get(i) instead. Is ok right?
ohh yeah, sorry, my mistake. I looked at your code to update my answer. I've fixed it in my answer now :)

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.