gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:38:50 25 4
gpt4 key购买 nike

我还是 Android 初学者,也是 Stackowerflow 的新手,希望你能理解我的问题。我使用 Volley 库和 Singleton 类。我有 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;
}}

然后我尝试调用 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;
}

我的arrayList始终为空。我希望有人能帮助我。

最佳答案

创建监听器

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

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

并命名它

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

});

关于java - 无法从另一个类获取 ArrayList。它总是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59986031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com