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.
Related
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
}
can someone tell me, how do get a Stringvalue from a thread to the mainActivity?
i have a thread like this:
public class XMLHandler extends DefaultHandler {
XMLDataCollected data = new XMLDataCollected();
......
......
public String getInformation() {
String information = "";
if (data.getData().equals("residential")) {
information = "Stadt";
}
return information;
}
}
in the mainActivity i tried to set the value into a textview like this:
textView.setText(xmlHandler.getInformation());
i does not work after all. what i am doing wrong? any solutions and advices? thanks in advance
If you have a SeparateThread class then you need to create one Interface say
public interface FetchValueListener{
public void sendValue(String value_to_send);
}
And your acctivity will be implementing this interface and thus sendValue(value_to_send) method will be added to your activity.
Next step would be when you create the object of the THread class then you need to pass the object of that interface in the paramater as follows:
public class myThreadClass{
FetchValueListener mllistener;
myThreadClass(FetchValueListener listenerObj){
mllistener=listenerObj;
}
}
Now when you want to send some value to the activity from thread you can just simply call
mllistener.sendValue(value_you_wan_to_send);
And inside your actiivty you will get the value in the sendValue() method..
In that method you need to post the data to runnable using the handler so that you can make changes to the UI like setText etc.....
If you directly try to set the value of text view in that method you will get an exception.
I have an outer class method that executes an inner class extending AsyncTask, and I want that outer method to then sleep till the AsycnTask tells it to continue. Basically, I am pulling something from a DB in the inner class, and calling it from the outer class, however I need the outer class to wait till it has actually been retrieved before it tires to access it.
public class A{
String response;
public String returnResponse{
new B().execute();
// wait for signal from B
return response;
}
private class B extends AsyncTask<String, Void, Void>{
response = string pulled from online db;
//once response has been set, signal A.returnResponce to stop waiting
}
Any ideas?
Thanks,
use
new B().execute().get();
instead of
new B().execute();
for make waiting until AsyncTask execution complete
use this
new B().execute().get();
instead of your code
new B().execute();
this will wait for signal from B.
Ok so now I have Class A that contains some spinners that values will be populated by Class B that extends AsnycTask which grabs the spinner values from a web service. In class B i manage to retrieve the values, showing in a Toast. The problem now is how do I pass those spinner values back to Class A?
I've tried
Can OnPostExcecute method in AsyncTask RETURN values?
by passing Class A to Class B and store the value in a public variable of Class A like below
#Override
protected void onPostExecute(String result)
{
classA.classAvariable = result;
}
However whenever I try to read the classAvariable i always get a NullPointer Exception.
Seems like the variable was never assigned with the result.
For readability purpose I needed to seperate Class B instead of using as an inline class.
Any ideas my fellow Java programmers?
Problem here is that when you execute your AsynchTask, its doInBackground() methode run in separate thread and the thread that have started this AsynchTask move forward, Thereby changes occur on your variable by AsynchTask does not reflect on parent thread (who stated this AsynchTask) immediately.
Example --
class MyAsynchTask
{
doInbackground()
{
a = 2;
}
}
int a = 5;
new MyAsynchTask().execute();
// here a still be 5
Create a interface like OnCompletRequest() then pass this to your ClassB constructor and simply call the method inside this interface such as complete(yourList list) in the method of onPostExecute(String result)
You can retrieve the return value of protected Boolean doInBackground() by calling the get() method of AsyncTask class :
E.g. you have AsyncTask class as dbClass like
dbClass bg = new dbClass(this);
String Order_id = bg.execute(constr,data).get();
Here I am passing constr as URL and data as string of inputs to make my class dynamic.
But be careful of the responsiveness of the UI, because get() waits for the computation to complete and will block the UI thread.
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.