There is a method initData (urisImg) which should create a list of Cover objects. The incoming data is the ArrayList urisImg list, which is initialized in the onCreate method and filled with values ​​each time the File is loaded by the FileLoader () method. I can not understand why my list size urisImg after downloading files becomes zero. That is, the initData method does not start working, because the urisImg list is empty ... Please help me find the error. Code below

import android.net.Uri; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.widget.Toast; import com.thin.downloadmanager.DownloadRequest; import com.thin.downloadmanager.ThinDownloadManager; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private String filename; private String fileUrl; private ArrayList<Uri> urisImg; private ArrayList<String> urls; private static final String LOG_TAG = "my_log"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recyclerview); LinearLayoutManager llm = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); recyclerView.setLayoutManager(llm); recyclerView.setHasFixedSize(true); urisImg = new ArrayList<>(); File file = new File(this.getCacheDir().getPath() + File.separator + "booklist.json"); if (!file.exists()){ new ParseBookCovers().execute(); } else { CoverFormer(); //посмотреть потом, будет ли успевать выполнять код при большом количестве обложек } initializeAdapter(); } private List<Cover> initData(ArrayList<Uri> urisImg) { ArrayList<Cover> covers = new ArrayList<>(); for (int i=0; i<urisImg.size();i++){ covers.add(new Cover(urisImg.get(i))); } return covers; } private void initializeAdapter() { RecyclerAdapter recyclerAdapter = new RecyclerAdapter(initData(urisImg)); recyclerView.setAdapter(recyclerAdapter); } private void FileLoader(String fileUrl, String filename) { ThinDownloadManager downloadManager = new ThinDownloadManager(5); //количество потоков загрузки Uri downloadUri = Uri.parse(fileUrl); Uri destinationUri = Uri.parse(filename); DownloadRequest downloadRequest = new DownloadRequest(downloadUri).setDestinationURI(destinationUri); downloadManager.add(downloadRequest); urisImg.add(destinationUri); } private class ParseBookCovers extends AsyncTask<Void, Void, String> { HttpURLConnection urlConnection = null; BufferedReader reader = null; String resultJson = ""; @Override protected String doInBackground(Void... params) { // получаем данные с внешнего ресурса try { URL url = new URL("http://******.ru/todbook/booklist.json"); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); InputStream inputStream = urlConnection.getInputStream(); StringBuffer buffer = new StringBuffer(); reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } resultJson = buffer.toString(); MyJSON.saveData(getApplicationContext(), resultJson); } catch (Exception e) { e.printStackTrace(); } return resultJson; } @Override protected void onPostExecute(String strJson) { CoverFormer(); } } private ArrayList<String> GetListUrlCovers(String strJson) { JSONObject dataJsonObj; ArrayList<String> urls = new ArrayList<>(); try { dataJsonObj = new JSONObject(strJson); JSONArray books = dataJsonObj.getJSONArray("books"); for (int i = 0; i < books.length(); i++) { JSONObject book = books.getJSONObject(i); String url_book = book.getString("coverUrl"); urls.add(url_book);// пишу урлы в ArrayList urls Log.d(LOG_TAG, "coverUrl: " + url_book); } } catch (JSONException e) { e.printStackTrace(); } return urls; } private void CoverFormer(){ MyJSON.getData(getApplicationContext()); //read file json urls = GetListUrlCovers(MyJSON.getData(getApplicationContext())); // parse urls + write to array for (int i = 0; i < urls.size(); i++) { int d = i + 1; fileUrl = urls.get(i); filename = this.getFilesDir() + File.separator + "bookcover_" + d + ".jpg"; FileLoader(fileUrl, filename); } } } 

    1 answer 1

    And when will he have time to fill?

    This is an asynchronous operation, it needs time to execute.

     new ParseBookCovers().execute(); 

    And right after its launch, you already want to see the results in the adapter.