0

I hope the following makes sense...

I have class A which does the below:

Class A

public class a extends fragment {

    private List<AllUserAdoptIdsData> dataListAdoptIds;

        public View onCreateView(.... {

          dataListAdoptIds = new ArrayList<>();

          populateAdoptIdsData("a");
       }

public void populateAdoptIdsData(final String email) {

    dataListAdoptIds.clear();

    AsyncTask<Object,Void,Void> task = new AsyncTask<Object, Void, Void>() {

        @Override
        protected Void doInBackground(Object... params) {

            String email = (String)params[0];

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("http://localhost/app_scripts/getUserAdoptPostIds2.php?email="+email )
                    .build();
            try {
                Response response = client.newCall(request).execute();

                JSONArray array = new JSONArray(response.body().string());

                for (int i=0; i<array.length(); i++){

                    JSONObject object = array.getJSONObject(i);

                    AllUserAdoptIdsData data = new AllUserAdoptIdsData(
                            object.getInt("ADOPT_ID")
                    );
                    dataListAdoptIds.add(data);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                System.out.println("End of content"+e);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            adapter.notifyDataSetChanged();
        }
    };
    task.execute(email);
  }
}

AllUserAdoptIdsData

package com.example.admin.paws;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class AllUserAdoptIdsData {
    public int adopt_id;
    private List<Integer> list;

    public AllUserAdoptIdsData(int adopt_id)
    {
     list = new ArrayList<Integer>();
     list.add(adopt_id);
     this.adopt_id = adopt_id;

        Log.d("LOG", "AllUserAdoptIdsData: "+list.size());
    }

    public int getId() {
        return adopt_id;
    }

    public List<Integer> getList() {
        return list;
    }

}

Now I have class B in which I want to get the values added to AllUserAdoptIdsData in class A. As you can see I tried to follow this link (Java - How to access an ArrayList of another class?), but obviously every time AllUserAdoptIdsData is initiated it creates a new array. Not sure where I'm going wrong.

Class B

public class B extends Fragment {

   public View onCreateView(....{

   //Here I'm trying to see if the list contains a specific value.

        AllUserAdoptIdsData allUserAdoptIdsData;
        allUserAdoptIdsData = new AllUserAdoptIdsData(0);

        List<Integer> list = allUserAdoptIdsData.getList();
        Log.d("LOG", "lookupAdoptId: "+list.contains(209));
   }
1
  • Store your values, in saveInstanceState or other appropriate mechanism. Commented Apr 20, 2018 at 10:06

2 Answers 2

0

...but obviously every time AllUserAdoptIdsData is initiated it creates a new array.

that's because you instantiate it every time when you call

AllUserAdoptIdsData allUserAdoptIdsData;
allUserAdoptIdsData = new AllUserAdoptIdsData(0);

To access the contents of the array(list) from a different class (especially different Fragments) you need to think of a way to provide this data between different Activities/Fragments (see this answer).

Also don't confuse Array and (Array)List!

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

Comments

0

See you have created a custom list using Model class.Please understand following point

  • In java constructor is use for initialise objects - you are initialising the int values through constructor of Model class to 0 every time.
  • There is a access specifier of an object which define their accessibility of specific object - you want to access list data from fragment a to fragment b so you need to declare public access specifier to list.
  • Once you have initialise any object to a specific values then previously initialised values get overwrite - in your Fragment b you are initialising your object to new object with some values every time it get initialised.

Comments

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.