简体   繁体   English

无法从另一个类获取 ArrayList。 它总是空的

[英]Cant get ArrayList from another class. Its always empty

im still Android beginner and new to Stackowerflow, i hope you will understand my problem.我仍然是 Android 初学者和 Stackowerflow 的新手,我希望你能理解我的问题。 Im using Volley libary and Singleton class.我使用 Volley libary 和 Singleton 类。 I have JsonParser class which after parsing should return the filled list with objects.我有 JsonParser 类,解析后应该返回包含对象的填充列表。

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.然后我尝试调用 getList 并获取我的列表 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.我的arrayList总是空的。 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.您的方法 getList() 调用 Web 服务,该服务在单独的线程上执行。 When you use MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);当你使用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.今天Google推荐的做法是使用LiveData,对于初学者来说可能有点难,但只要稍微研究一下,就可以解决。

https://developer.android.com/topic/libraries/architecture/livedata 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如果对我有帮助,请告诉我,谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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