-2

I have a problem updating a TextView on my MainActivity form a class which is Async I have seen that I need to set the Context of the second class to that of the MainActivity but I don't know how to achieve this in this scenario. My app looks like this.

 class RestOperation  extends AsyncTask<String, Void, Void>  {

    @Override
    protected Void doInBackground(String... params) { 
    //Java.net Http    transaction happens here 
    }

    @Override
    protected void onPostExecute(Void result) {
        //I wish to set the value of the TextView with the result here!
    }
Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49
joebohen
  • 145
  • 1
  • 14

6 Answers6

1

You can do it like this :

#1 : Make a constructor for your RestOperation class to get the Context from your Activity like this :

    public RestOperation(MainActivity activityContext){
        this._activityContext = activityContext;
    }

#2 : Pass the Activity context while initializing the AsyncTask class like this :

new RestOperation(MainActivity.this).execute("yours","params","here");

So, your AsyncTask class should look like this :

class RestOperation  extends AsyncTask<String, Void, String>  {
    MainActivity _activityContext;
    public RestOperation(MainActivity activityContext){
        this._activityContext = activityContext;
    }
    @Override
    protected String doInBackground(String... params) { 
        //Java.net Http    transaction happens here 
    }

    @Override
    protected void onPostExecute(String result) {
        ((TextView)_activityContext.findViewById(R.id.yourTextViewId)).setText(yourText);
    }
}
Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
1
  1. It'll be good if you will create interface which will be call at onPostExecute and at Main activity implement that interface and get your value and set value with textview.

2.newrestOperation(YourActivity.this,textviewobject).execute("yours","params","here");

So, your AsyncTask class should look like this :

Textview mtxt;
class RestOperation  extends AsyncTask<String, Void, Void>  {
    Context _activityContext;
    public RestOperation(Context activityContext,Textview txt){
        this._activityContext = activityContext;
         this.mtxt = txt;
    }
    @Override
    protected String doInBackground(String... params) { 
        //Java.net Http    transaction happens here 
    }

    @Override
    protected void onPostExecute(String result) {enter code here
        mtxt .setText(yourText);
    }
}

But #1 is better way.

Community
  • 1
  • 1
ViramP
  • 1,659
  • 11
  • 11
0

Simply use:

@Override
protected void onPostExecute(String result)
    {
        textView.setText(result);
    }
Jas
  • 3,207
  • 2
  • 15
  • 45
  • I am new to Android so please bear with me! RestOperation is a separate class so textview is not available in OnPostExecute. If I attempt to add a reference to the TextView I get cannot resolve findViewById! – joebohen Jul 18 '16 at 10:07
0

Change your code to:

class RestOperation  extends AsyncTask<String, Void, Void>  {

Context context; 
add public RestOperation (Contex cntx) {
this.context = cntx;
}
   @Override
    protected Void doInBackground(String... params) { //Java.net Http    transaction happens here }

@Override
   protected void onPostExecute(Void result) {
        //I wish to set the value of the TextView with the result here!
    }

And when you cat this task

new ResrOperation(getApplicationContext()).excute();

Then you can access to items from main activity

Another choise

You can Overide onPostExecute method in your main activiry and then you will have acces to activity elemets too (In this case no context nedded)

new ResrOperation(){
@Overide 
void onPostExecute(Long result) {
     // access elemets from activity
 }
}.excute();
0

First write an interface in the Async class

CompleteListener completeListener;

public void setListener(CompleteListener cmpltListnr) {
   this.completeListener = cmpltListnr;
}

public interface CompleteListener {
    void OnCompleteListener(String result);
}   

and onPostExecute

@Override
protected void onPostExecute(Void result) {
    if ( completeListener != null ) {
        completeListener.OnCompleteListener(""+result);
    }
}

and implement the listener in the Activity you want. and override the function and assign this to the interface by

 yourAsynClassObject.setListener(this);

 @Override
 public void OnCompleteListener(String result) {
    textview.setText("" + result);
 }
Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49
0

If this is in another class, you should get a reference to MainActivity through the constructor (note that I use WeakReference here so that when the activity is destroyed the onPostExecute won't be executed (because it does not need to) and the AsyncTask won't leak memory)

//first you have to change the param (the last one) to String so your doInBackground would return String
class RestOperation  extends AsyncTask<String, Void, String>  {
private WeakReference<MainActivity> weakRef;

public RestOperation(MainActivity activity) {
    weakRef = new WeakReference<MainActivity>(activity);
}

@Override
protected String doInBackground(String... params) { 
//Java.net Http transaction happens here 
//You need to return some value here to set to the text view later
return "Something";
}

@Override
protected void onPostExecute(String result) {
    MainActivity activity = weakRef.get();
    if (activity != null) {
        //activity is still alive, updates the text view
        activity.updateText(result);
    }
}

In your MainActivity:

new RestOperation(this).execute("Your input param");

also make another method in MainActivity:

public void updateText(String result) {
    textView.setText(result);
}
Kise
  • 2,823
  • 1
  • 24
  • 43
  • Everthing compiles apart form 'new RestOperation(this).execute("Your input param"); this is underlined in red and I get : RestOperation (mainActivity) in RestOperation cannot be applied. – joebohen Jul 18 '16 at 14:12
  • Ah found the correct way to call the operation; new RestOperation(MainActivity.this).execute("Your input param"); Thank you. – joebohen Jul 18 '16 at 16:30