textview and button in my listview by using adapter class. When i click on that button i have to call AsyncTask passing parameters i.e String of that perticular position in adapter class getview method .Here am created my Asynctask is another class i.e an activity class. Please provide some examples.
Thanks in advance.
Your AsycnTask takes an Array of some sort - Strings for instance, so when you instantiate the AsyncTask, you just pass it an Array like so:
String[] arr = new String[] {"A string to pass..."};
MyAsyncTask task = new MyAsyncTask();
task.execute(arr);
Full example on how to use it:
http://www.android-ever.com/2012/10/android-asynctask-example.html
Create public class in different file or within your activity. If you are creating within Activity then define like this way
public class static MyAsync extends AsyncTask<String, Void, String>{
}
and then used anywhere like this
YourActivity.MyAsync myAsync = new YourActivity.MyAsync();
for passing the value to the your Async class use this way
myAsync.execute(yourstring);
access in doInBackground like this way
public String doInBackground(String... param){
String s = param[0]; // here you can access you string like this way
}
Related
I have created an AsyncTask and I set it as static like this
public static class CreateLiveEventTask extends AsyncTask<Void, Void, List<EventData>>
Now I want to add a ProgressDialog to onPreExecute() , so I write some code like this
progressDialog = ProgressDialog.show(this, null,
getResources().getText(R.string.loadingEvents), true);
But it results in error and said that I need to change AsyncTask to not static . I need this AsyncTask at the other Activity , how can I do to solve this ?
Can not create outer class as Static in java
Why can't we have static outer classes
This is reference link
Change AsyncTask to non static, & your can create an object of the class containing "CreateLiveEventTask" in the other activity to access CreateLiveEventTask.
Hope it'll help
I can use AsyncTask as an inner class to get the returned result then assign it to the global String var.
But now my idea is to use the AsyncTask like this. My caller method and MyAsyncTask class are different classes:
//in my caller class
String returned_string = new MyAsyncTask().execute(myparams); //The MyAsyncTask class returns the string after it's done everything.
Log.v("RETURNED STRING",returned_string); //Check if the returned string is correct
But of course it cannot be done because they are in different type.
I don't know if my idea is possible?
Thank you for your time.
I am trying to define and set this adapter in an oncreate method. The stockArray is an ArrayList defined in the main activity (not the same class). I am getting an error which says my constructor is undefined.
ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(SearchActivity.this, android.R.layout.simple_list_item_1, MainActivity.stockArray);
This is how the array is defined in main activity:
public static AsyncTask<ArrayList<Object>, Void, ArrayList<Object>> stockArray;
you are getting an error because your arrayadapter is of type "ArrayList<Object>" and you are passing an instance of type AsyncTask<ArrayList<Object>, Void, ArrayList<Object>>
Try passing an instance of type ArrayList<Object> to your constructor.
Hope this helps.
stockArray in your case is not an array but this
AsyncTask<ArrayList<Object>, Void, ArrayList<Object>>
In last argument you need to pass an object of type ArrayList<Object>;
If your MainActivity is an Activity object then you can't access an Array in that way you have done. You can keep your Array as an static object in another class , then use in your Activity with static access.
public class Constants {
public static final Object [] myArray = { object1 , object2 } ;
}
then in your Activity
ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(SearchActivity.this, android.R.layout.simple_list_item_1, Constants.myArray);
Hope it helps.
I have an AsyncTask with a textview loaded in the class so it looks something like this:
private class MyClass extends AsyncTask<TextView, Void, Void>{
}
TextView tv;
Loaded this way
"new MyClass(tv).execute();"
The reason for this is because of I have a textview loaded inside a viewflipper and I have a long load method inside the task to implement a process dialog.
My error is found on "protected Void doInBackground(TextView... params) {" this is where params is a TextView[] but not a single TextView.
Does anyone have a solution of to this problem?
Your TextView is the first element in params:
TextView tv = params[0];
NOTE:
If you plan to modify that TextView in doInbackground() don't do it because you'll throw an exception(you can't modify a view from another thread, instead use the onPostExecute method).
You can't change the method signature of the AsyncTask.doInBackground() method.
It is defined to take a varargs parameter, so you will have to pass a TextView[] parameter to your AsyncTask.
Try
Arrays.asList(tv);
If you want to pass a single TextView you will need to define a Construcotr in MyClass and then store the TextView as a field in MyClass. Beware doing this though, you should not hold references to View's or Contexts in your tasks. This will stop the Android OS from garbage collecting the activity that owns the TextView and could leat to a memory leak. If you must keep a reference to a view or context in your AsyncTask, use something like this:
private class MyClass extends AsyncTask<TextView, Void, Void>{
private WeakReference<TextView> tvRef;
public MyClass(TextView tv) {
this.tvRef = new WeakReference<TextView>(tv);
}
}
Just read params[0] as your parameter. You can pass a single value, a sequence or an array wherever you see ... in the parameter list. (A single value is actually a sequence containing just one element.)
I want to implement a generic, thread save class which takes the RessourceId of an ImageView and the Url (http) where the desired image file is stored. It'll download the image and fills the src of the ImageView in the UiThread.
I thought AsyncTask would be the best thing for me. However I noticed that I only can pass one type of parameters to the doInBackground() Method. Like an Array of Urls. Is that true? What would u suggest me?
You can pass params as objects
new MyTask().execute(url, str, context);
public class MyTask extends AsyncTask<Object, Void, Void> {
#Override
protected Void doInBackground(Object... params) {
Url url = (Url) params[0];
String str = (String) params[1];
Context ctx = (Context) params[2];
return null;
}
}
You can add setter methods to your AsyncTask implementation, or even define your own constructor to pass additional parameters.
Optionally, if your AsyncTask implementation is an inner class of an activity you can access all the instance variables of your activity. I prefer the above option myself, as it clearly indicates which data the task requires.