添加项目到arraylist后Android截击在添加项目后仍然为空
我在我的android应用程序中使用截取,我将Torrent对象添加到Arraylist,它填充了列表,但在程序退出此方法getAllDetails()后,Arraylist为空。有人能解释一下到底发生了什么吗添加项目到arraylist后Android截击在添加项目后仍然为空,android,multithreading,android-volley,Android,Multithreading,Android Volley,我在我的android应用程序中使用截取,我将Torrent对象添加到Arraylist,它填充了列表,但在程序退出此方法getAllDetails()后,Arraylist为空。有人能解释一下到底发生了什么吗 private void getAllDetails() { String URL = MOVIE_DETAILS_URL + movie.getId() + CAST_URL; JsonObjectRequest jsonObjectRequest = new Json
private void getAllDetails() {
String URL = MOVIE_DETAILS_URL + movie.getId() + CAST_URL;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject dataObject = response.getJSONObject(Keys.DATA);
JSONObject movieObject = dataObject.getJSONObject(Keys.MOVIE);
JSONArray torrentsArray = movieObject.getJSONArray(Keys.TORRENTS);
for (int i = 0; i < torrentsArray.length(); i++) {
JSONObject torrentObject = torrentsArray.getJSONObject(i);
Torrent torrent = new Torrent();
torrent.setUrl(torrentObject.getString(Keys.URL));
torrent.setSize(torrentObject.getString(Keys.SIZE));
torrent.setQuality(torrentObject.getString(Keys.QUALITY));
torrent.setSeeds(Integer.parseInt(torrentObject.getString(Keys.SEEDS)));
torrent.setPeers(Integer.parseInt(torrentObject.getString(Keys.PEERS)));
torrentList.add(torrent);
}
getTorrent();//when this method is called here the list has items on it and it works fine
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObjectRequest);
}
您可以尝试将
ArrayList
传递给getTorrent()
函数
getTorrent(torrentList);
您将这样调用函数
private void getTorrent(ArrayList<Torrent> passedList) {
String mUrl = passedList.get(0).getUrl();
// rest of your code here
}
private void getTorrent(ArrayList passedList){
字符串mUrl=passedList.get(0.getUrl();
//剩下的代码在这里
}
但是你需要知道,这个函数总是会给你第一个torrent的结果。因为您将在
ArrayList
中获得0
索引。也许通过传递索引,您还可以创建更多的函数方法。一个快速修复方法,您可以尝试将ArrayList
传递给getTorrent()
函数
getTorrent(torrentList);
您将这样调用函数
private void getTorrent(ArrayList<Torrent> passedList) {
String mUrl = passedList.get(0).getUrl();
// rest of your code here
}
private void getTorrent(ArrayList passedList){
字符串mUrl=passedList.get(0.getUrl();
//剩下的代码在这里
}
但是你需要知道,这个函数总是会给你第一个torrent的结果。因为您将在ArrayList
中获得0
索引。也许通过传递索引,您可以创建更多的函数方法