Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>We can reuse Aysntask with different parameters. For this</p> <p>1.Create an Interface so that we can reuse,pass and receive parameters</p> <pre><code>public interface BackgroundListener { public Object[] startBackgroundWork(Object... objs); public void endBackgroundWork(Object... objs); public void beforeBackgroundWork(); } </code></pre> <p>2.Create a Class Extending Asyntask</p> <p>BackgroundHandler.java</p> <pre><code>import android.os.AsyncTask; public class BackgroundHandler extends AsyncTask&lt;Object, Object[], Object[]&gt;{ BackgroundListener backgroundListener; public void setBackgroundListener(BackgroundListener aBackgroundListener) { this.backgroundListener = aBackgroundListener; } @Override protected void onPreExecute() { backgroundListener.beforeBackgroundWork(); } @Override protected Object[] doInBackground(Object... objs) { return backgroundListener.startBackgroundWork(objs); } @Override protected void onPostExecute(Object result[]) { backgroundListener.endBackgroundWork(result); } </code></pre> <p>}</p> <p>Using in Activity</p> <p>A.java</p> <pre><code>Class A extends Activity implements BackgroundListener { ...onCreate() { BackgroundHandler backgroundHandler = new BackgroundHandler() backgroundHandler.setBackgroundListner(this); backgroundHandler.execute(new Object[]{url1});//pass any number of parameters of any object type // show loading bar } public void beforeBackgroundWork() { pDialog = new ProgressDialog(A.this); pDialog.setMessage("Loading Data. Please wait..."); pDialog.setIndeterminate(false); ..... } public Object[] startBackgroundWork(Object... objs) { // access and type convert the passed parameters like objs[0], objs[1] //.... some time consuming stuff //.... some time consuming stuff String url_foralldropdowns = objs[0].toString(); List&lt;NameValuePair&gt; params1 = new ArrayList&lt;NameValuePair&gt;(); JSONObject json = jparser.makeHttpRequest(url_foralldropdowns, "GET", params1); JSONArray compoment = json.getJSONArray(COMPONENT_CODE); //Create new list_compoment here instead of global declaration for (int i = 1; i &lt; compoment.length(); i++) { JSONObject c = compoment.getJSONObject(i); String code = c.getString(CODE); list_compoment.add(code); } retrun new Object[]{list_compoment}; } public void endBackgroundWork(Object ...obj) { pDialog.dismiss();// hide loading bar //access resultant parameters like objs[0], objs[1] //user list_component will be in obj[0] } } </code></pre> <p>Similarly we can reuse in B.java</p> <pre><code>Class B extends Activity implements BackgroundListener { ... .... public void beforeBackgroundWork() { pDialog = new ProgressDialog(B.this); pDialog.setMessage("Loading Data. Please wait..."); pDialog.setIndeterminate(false); ..... } public Object[] startBackgroundWork(Object... objs) { // access and type convert the passed parameters like objs[0], objs[1] //.... some time consuming stuff //.... some time consuming stuff String url2 = objs[0].toString(); List&lt;NameValuePair&gt; params1 = new ArrayList&lt;NameValuePair&gt;(); JSONObject json = jparser.makeHttpRequest(url2, "GET", params1); JSONArray compoment = json.getJSONArray(COMPONENT_CODE); //Create new list_compoment here instead of global declaration for (int i = 1; i &lt; compoment.length(); i++) { JSONObject c = compoment.getJSONObject(i); String code = c.getString(CODE); list_compoment.add(code); } retrun new Object[]{list_compoment}; } public void endBackgroundWork(Object ...obj) { pDialog.dismiss(); ..... //user list_component will be in obj[0] } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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