Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should insert the data in the <code>onPostExecute</code> as follows:</p> <pre><code>public class ImportExport extends Activity { public void webimport(View v){ new RemoteConnectivity().execute(); // START OF ASYNCTASK } final ArrayList&lt;String[]&gt; importdata = new ArrayList&lt;String[]&gt;(); private class RemoteConnectivity extends AsyncTask&lt;Void, Void, Void&gt; { @Override protected Void doInBackground(Void... arg0) { .................. .................. importdata.add(dataline); // POPULATE ARRAYLIST IMPORTDATA return null; } @Override protected void onPostExecute(Void result1) { //INSERT importdata INTO DATABASE AFTER EXECUTE FINISH (POST EXECUTE) mylibmandbhandler db = new mylibmandbhandler(ImportExport.this); for (String[] s : importdata){ db.addRecord(new mylibman(s)); } } } } </code></pre> <p>The reason is because, onPostExecute is the method (callback) which is executed after the data has been done downloading in the <code>doInBackground</code> method.</p> <p><strong>Alternatively</strong>: You can also put the <code>insert</code> statements inside the <code>doInBackground</code> if you want the inserting to be done by background thread. This may be helpful, if inserting into the database is time consuming. In such a case the UI thread won't block. '</p> <pre><code>public class ImportExport extends Activity { public void webimport(View v){ new RemoteConnectivity().execute(); // START OF ASYNCTASK } final ArrayList&lt;String[]&gt; importdata = new ArrayList&lt;String[]&gt;(); private class RemoteConnectivity extends AsyncTask&lt;Void, Void, Void&gt; { @Override protected Void doInBackground(Void... arg0) { .................. .................. importdata.add(dataline); // POPULATE ARRAYLIST IMPORTDATA //INSERT importdata INTO DATABASE. NOW DONE IN THE BACKGROUND THREAD (Alternate method) mylibmandbhandler db = new mylibmandbhandler(ImportExport.this); for (String[] s : importdata){ db.addRecord(new mylibman(s)); } return null; } @Override protected void onPostExecute(Void result1) { } } } </code></pre> <p><strong>Update</strong>: The important this to highlight in my answer is that I used <code>new mylibmandbhandler(ImportExport.this);</code> instead of <code>new mylibmandbhandler(this);</code> as <code>this</code> refers to <code>AsyncTask</code> which is not what the constructor supports. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload