Depending on the presence of files in the folder, the button changes its text "Read / Download". If the files are in place, immediately setText "Read" and go to another activity. If there are no files, setText "Load" and load the files. Then we change the inscription to "Read". But when pressed again, the handler re-launches the file download. Tell me, please, how to do it. Here is the code:

if(numbFiles == 0) { buttonDownload.setText(R.string.buttonDownload); /** * Π—Π°Π³Ρ€ΡƒΠ·ΠΈΠΌ json с url Ρ„Π°ΠΉΠ»ΠΎΠ² ΠΊΠ½ΠΈΠ³ΠΈ */ Thread jsDownload = new Thread(new Runnable() { @Override public void run() { BookLoader(); } }); jsDownload.start(); // запустили ΠΏΠΎΡ‚ΠΎΠΊ 1 try { } catch (Exception e) { e.printStackTrace(); } /** * ΠžΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅ΠΌ Π½Π°ΠΆΠ°Ρ‚ΠΈΠ΅ ΠΊΠ½ΠΎΠΏΠΊΠΈ "Π—Π°Π³Ρ€ΡƒΠ·ΠΈΡ‚ΡŒ" ΠΈ Π³Ρ€ΡƒΠ·ΠΈΠΌ Ρ„Π°ΠΉΠ»Ρ‹ ΠΊΠ½ΠΈΠ³ΠΈ */ buttonDownload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Thread mThread = new Thread(new Runnable() { @Override public void run() { String fileListB = "list_" + "book_" + bookId + ".json"; String jsReadFile = MyJSON.getData(getApplicationContext(), fileListB); Log.d(TAG, jsReadFile); Gson gson = new Gson(); Book book = gson.fromJson(jsReadFile, Book.class); List<String> pages = book.getPageUrl(); String[] urlsPages = pages.toArray(new String[0]); DownloadFilesBook(urlsPages); } }); mThread.start(); // запустили ΠΏΠΎΡ‚ΠΎΠΊ 2 buttonDownload.setText(R.string.buttonRead); } }); } else { buttonDownload.setText(R.string.buttonRead); buttonDownload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NextActivity(); } }); 
  • And where do you change numbFiles? - Yura Ivanov
  • this is the count of files in the folder, it immediately triggers when the activity starts - Vyacheslav
  • and never changes anywhere. you will always have the first handler to work. - Yura Ivanov
  • no, not so, everything changes but you need to return to the previous activism - Vyacheslav
  • But I understand, you are offering to ply the counter. Then I need to wrap everything up in a loop. I try - Vyacheslav

1 answer 1

The solution turned out to be the insertion of a listener code for reading after the launch of stream 2:

  buttonDownload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Thread mThread = new Thread(new Runnable() { @Override public void run() { String fileListB = "list_" + "book_" + bookId + ".json"; String jsReadFile = MyJSON.getData(getApplicationContext(), fileListB); Log.d(TAG, jsReadFile); Gson gson = new Gson(); Book book = gson.fromJson(jsReadFile, Book.class); List<String> pages = book.getPageUrl(); String[] urlsPages = pages.toArray(new String[0]); DownloadFilesBook(urlsPages); } }); mThread.start(); // запустили ΠΏΠΎΡ‚ΠΎΠΊ 2 buttonDownload.setText(R.string.buttonRead); buttonDownload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NextActivity(); } }); } }); } else { buttonDownload.setText(R.string.buttonRead); buttonDownload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NextActivity(); } });