Reputation: 1889
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.
Upvotes: 3
Views: 20052
Reputation: 8488
The answer to all your questions is an AsyncTask
logic:
While defining AsyncTask
, we need to declare 3 parameters:
private class MyTask extends AsyncTask<Object, Object, Cursor>
Like in your case above, you have given Object, Object, Cursor.
Now, 1st parameter is set during the "execute" call and is used by doInBackGround()
function. This has been designed to be an array of defined type so that we can send multiple data. For example, we can do:
myTask.execute(url1, url2, url3)
for my AsysncTask
whose 1st parameter is URL. In doInBackGround()
function, we can assess the as url[0], url[1], url[2]
2nd Parameter in AsyncTask
is used by onProgressUpdate()
function and it also expects to receive array of defined type.
3rd Parameter in AsyncTask
is used by onPostExecute()
function and is the value rerturned by doInBackGround()
function. In need not be an array of defined type.
Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void:
private class MyTask extends AsyncTask { ... }
The below link details about AsyncTask
which will answer all 3 question of yours:
http://developer.android.com/reference/android/os/AsyncTask.html
Upvotes: 3
Reputation: 8155
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.
Upvotes: 9