Android - AsyncTask class and Execute method
android
Solution
`AsyncTask` has the form `AsyncTask<Param, Progress, Return>` with `execute` using the `Param` type.
#1
It is possible to write `execute(null)`. Doing so results in the `Confusing 'null' argument to var-arg method` warning in IntelliJ IDEA. This warning notes that it is not wrapped as a single-element array. Therefore, as `Param` is `Object` in this case, `execute((Object[])null)` is used to supress the warning.
Since variable argument parameters don't have to have a value passed to them, you can use `execute()` instead.
#2 & #3
In this case, `Param` is `Long`, so the execute method has the form `execute(Long...)`. Without the variable argument syntactic sugar, this is the same as `execute(Long[])`. Therefore, the `new Long[]{ rowId }` is explicitly creating the `Long` array with a single element (`rowId`). As `rowId` is a `long`, the Java compiler automatically 'boxes' (wraps) that integer into a `Long` object -- the equivalent of writing `new Long(rowId)`.
As the `execute` method uses variable arguments, you don't need to allocate the `Long` array directly -- the Java compiler will do this for you -- so you can just write `execute(rowId)` instead. That is, the Java compiler is expanding `execute(rowId)` into `execute(new Long[]{ new Long(rowId) })`.
Therefore, #2 and #3 are equivalent.
Problem
I have code using the AsyncTask class for populating a listView with several contacts from a database. ``` @Override protected void onResume() { super.onResume(); new MyTask().execute((Object[]) null); } // end method onResume ``` 1.- Why do I pass this: `(Object[]) null)` as an argument? See the AsyncTask code: ``` private class MyTask extends AsyncTask<Object, Object, Cursor> { //used for database conection purpose ConectToDatabase databaseConnector = new ConectToDatabase(ThisClass.this); @Override protected Cursor doInBackground(Object... params) { databaseConnector.open(); return databaseConnector.getMyContacts(); } // use the Cursor returned from the doInBackground method @Override protected void onPostExecute(Cursor result) { //My cursorAdadper defined in early code contactAdapter.changeCursor(result); databaseConnector.close(); } } ``` Another AsyncTask issue: ``` delete.execute(new Long[] { rowID }); ``` 2.- Why do I pass this: `(new Long[] { rowID })` as an argument and not a simple (rowId)? rowID is a a long type which contains the id of the contact selected in my previous class. That was extendend in a ListActivity. This previous class was populated by all my contacts obtained in my database. When I'm sent data in this class(by an intent) I want to recover the data of the single contact selected in my previous class, but in this case the code appears in this way: `new LoadMyContact().execute(rowID);` located in onResume method. 3.- Why do I pass only: `(rowID)` as an argument ? `delete.execute(new Long[] { rowID });` is inside a menu, when the user confirm delete the contact we execute that sentence the code will be this for the delete(we are inside on a click button): ``` @Override public void onClick(DialogInterface dialog, int button) { final ConectToDataBase databaseConnector = new ConectToDataBase(MySecondClass.this); AsyncTask<Long, Object, Object> deleteTask = new AsyncTask<Long, Object, Object>() { @Override protected Object doInBackground(Long... params) { databaseConnector.deleteContact(params[0]); return null; } @Override protected void onPostExecute(Object result) { finish(); delete.execute(new Long[] { rowID }); } }; // end new AsyncTask delete.execute(new Long[] { rowID }); } / ``` Help with this three question and thanks.