简体   繁体   中英

Cant get ArrayList from another class. Its always empty

im still Android beginner and new to Stackowerflow, i hope you will understand my problem. Im using Volley libary and Singleton class. I have JsonParser class which after parsing should return the filled list with objects.

public class JsonParse {

Context context;
public ArrayList<ParseItem> parseItemList = new ArrayList<>();
String json_url = "myurl";

public JsonParse(Context context) {
    this.context = context;

}

public ArrayList<ParseItem> getList() {

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, json_url, null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {


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


                        try {
                            JSONObject jsonObject = response.getJSONObject(i);
                            ParseItem parseItem = new ParseItem();


                            String naslov = jsonObject.getString("naslov");
                            String url = jsonObject.getString("url");

                            parseItem.setTitle(naslov);
                            parseItem.setUrl(url);

                            JSONArray niz = jsonObject.getJSONArray("niz");



                            ArrayList<String> podnaslovTMP = new ArrayList<>();
                            ArrayList<String> podurlTMP = new ArrayList<>();
                            for (int j = 0; j < niz.length(); j++) {

                                JSONObject nizOBJ = niz.getJSONObject(j);
                                String podnaslov = nizOBJ.getString("podnaslov");
                                String podurl = nizOBJ.getString("podurl");

                                podnaslovTMP.add(podnaslov);
                                podurlTMP.add(podurl);


                            }

                            parseItemList.add(parseItem);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }


                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });


    MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);

    return parseItemList;
}}

Then im trying to call getList and take my list i HomeFragment.

public class HomeFragment extends Fragment implements View.OnClickListener {


private Context context;
private static final int REQUEST_CALL = 1;
private View view;
private ParseItem parseItem = new ParseItem();
public ArrayList<ParseItem>arrayList = new ArrayList<>();

public HomeFragment() {
}


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_home, container, false);
    init();

    JsonParse jsonParse = new JsonParse(getActivity());
    arrayList = jsonParse.getList();


    return view;
}

My arrayList is always empty. I hope someone can help me.

create listner

public interface GetArrayListListner{
   void getArrayData(ArrayList<ParseItem>);
}

and

   public class JsonParse {
   private GetArrayListListner arrayListListner;// add this line
   Context context;
   public ArrayList<ParseItem> parseItemList = new ArrayList<>();
   String json_url = "myurl";

   public JsonParse(Context context,GetArrayListListner arrayListListner) {
    this.context = context;
    this.arrayListListner = arrayListListner; //Add this line

 }
 public void getList() {

   new JsonArrayRequest(Request.Method.POST, json_url, null,
        new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {


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


                    try {
                        JSONObject jsonObject = response.getJSONObject(i);
                        ParseItem parseItem = new ParseItem();


                        String naslov = jsonObject.getString("naslov");
                        String url = jsonObject.getString("url");

                        parseItem.setTitle(naslov);
                        parseItem.setUrl(url);

                        JSONArray niz = jsonObject.getJSONArray("niz");



                        ArrayList<String> podnaslovTMP = new ArrayList<>();
                        ArrayList<String> podurlTMP = new ArrayList<>();
                        for (int j = 0; j < niz.length(); j++) {

                            JSONObject nizOBJ = niz.getJSONObject(j);
                            String podnaslov = nizOBJ.getString("podnaslov");
                            String podurl = nizOBJ.getString("podurl");

                            podnaslovTMP.add(podnaslov);
                            podurlTMP.add(podurl);


                        }

                        parseItemList.add(parseItem);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
              arrayListListner.getArrayData(parseItemList);//add this line

            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
    }
});


MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);

return parseItemList;
 }
}

and call it

JsonParse jsonParse = new JsonParse(getActivity(),new GetArrayListListner(){
  @Overrid void getArrayData(ArrayList<ParseItem>){
        arrayList = jsonParse.getList();
   }

  });

Your method getList() calls a web service, which is executed on a separate thread. When you use MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest); you are placing the request to a Queue, that means it is not gonna be executed right now, but you are already returning your array. As the request hasn't been executed yet, your array list hasn't been filled yet.

The practice recommended by Google today is using LiveData, it can be hard to a beginner, but with a little study, you can solve it.

https://developer.android.com/topic/libraries/architecture/livedata

Maybe you can look at Reactive Programming as well.

Another easy solution would be using interfaces, so you can pass the interface as a parameter, which will be triggered when your array gets filled.

Let me know if I was of any help, thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM