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