한국어

Coding

온누리070 플레이스토어 다운로드
    acrobits softphone
     온누리 070 카카오 프러스 친구추가온누리 070 카카오 프러스 친구추가친추
     카카오톡 채팅 상담 카카오톡 채팅 상담카톡
    
     라인상담
     라인으로 공유

     페북공유

   ◎위챗 : speedseoul


  
     PAYPAL
     
     PRICE
     

pixel.gif

    before pay call 0088 from app


https://stackoverflow.com/questions/18234835/how-can-i-set-the-text-for-a-textview-from-an-asynctask-activity

Viewed 5k times
1

I have got a Fragment Activity that contains a textview and another class that extends AsyncTask. Now I would like to use the onPostExecute(String result) method to set the result text in my textview that is in my fragment activity.

How can I do that? I already created a custom constructor for the AsyncTask class that takes in a context object. How can I use that??

This is how I create a task object in my Fragment activity:

String query = "someText";
Task task = new Task(this.getActivity());
task.execute(query);

This is a snippet from my task class:

public class Task extends AsyncTask<String, Void, String> {

    private Context context;

    public Task (Context context) {
        this.context = context;
    }

    protected void onPostExecute(String result) {
    super.onPostExecute(result);
    // ??? What comes here ???
    }
}
5
TextView txt = (TextView)((Activity)context).findViewById(R.id.watheveryouwant);
txt.setText("blabla");

But you should pass an Activity and not a Context, will be easier ;-)

Or

    public Task (Context context, TextView t) {
        this.context = context;
        this.t = t;
    }
   super.onPostExecute(result);
        t.setText("BlahBlah")
    }

Should do the trick

0

You can pass to the AsynkTast the instance of the TextView as a parameter, and call setText in onPostExecute.

0

in your case, simply following will come here:

((TextView)findViewById(R.id.xyz)).setText("abc");
0

I picked the solution from

How do I return a boolean from AsyncTask?

new Task(getActivity()).execute(query);

In the Constructor of AsyncTask

TheInterface listener;
public Task(Context context)
{
  listener = (TheInterface) context; 
}

Interface

public interface TheInterface {

public void theMethod(String result); // your result type

 }

Then

In your doInbackground return the result.

In your onPostExecute

if (listener != null) 
{
  listener.theMethod(result); // result is the String
  // result returned in doInbackground 
  // result of doInbackground computation is a parameter to onPostExecute 
}

In your activity class or fragment implement the interface

public class ActivityName implements Task.TheInterface

Then

@Override
 public void theMethodString result) { 
    tv.setText(result); 
    // set the text to textview here with the result of background computation
    // remember to declare textview as a class member.
 }

Edit:

You are also missing @Override annotation for your onPostExecute

조회 수 5381
조회 수 5289
FCM 푸시 메세지 전송
admin
2019.09.27
조회 수 5640
조회 수 7438
조회 수 8011
조회 수 6414
조회 수 7255
조회 수 7779
조회 수 7318
조회 수 11205
조회 수 6705
조회 수 7804
조회 수 7092
조회 수 6924
android apk 패키징 v1, v2
admin
2018.12.05
조회 수 7470
조회 수 7178
조회 수 6938
조회 수 7074
조회 수 8010
SDK Platform Release Notes
admin
2018.05.13
조회 수 7367
sdk-tools list
admin
2018.05.13
조회 수 7372
조회 수 7834
조회 수 7758
조회 수 7270
조회 수 7411
Firebase용 Cloud 함수
admin
2018.04.26
조회 수 7772
안드로이드 알람
admin
2018.02.23
조회 수 7956
조회 수 7900
조회 수 7848
gcm 코딩 사례
admin
2018.01.09
조회 수 7715
조회 수 12413
조회 수 8118
조회 수 8773
조회 수 8160
조회 수 10224
FCM PHP Curld
admin
2018.01.01
조회 수 8691
FCM 과 GCM 차이
admin
2018.01.01
조회 수 10302
조회 수 8041