한국어

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

조회 수 5880
조회 수 5762
FCM 푸시 메세지 전송
admin
2019.09.27
조회 수 6117
조회 수 7926
조회 수 8476
조회 수 6877
조회 수 7728
조회 수 8248
조회 수 7785
조회 수 11657
조회 수 7150
조회 수 8276
조회 수 7560
조회 수 7405
android apk 패키징 v1, v2
admin
2018.12.05
조회 수 7935
조회 수 7649
조회 수 7420
조회 수 7524
조회 수 8473
SDK Platform Release Notes
admin
2018.05.13
조회 수 7839
sdk-tools list
admin
2018.05.13
조회 수 7839
조회 수 8315
조회 수 8237
조회 수 7742
조회 수 7871
Firebase용 Cloud 함수
admin
2018.04.26
조회 수 8258
안드로이드 알람
admin
2018.02.23
조회 수 8409
조회 수 8365
조회 수 8306
gcm 코딩 사례
admin
2018.01.09
조회 수 8187
조회 수 12874
조회 수 8565
조회 수 9226
조회 수 8637
조회 수 10681
FCM PHP Curld
admin
2018.01.01
조회 수 9171
FCM 과 GCM 차이
admin
2018.01.01
조회 수 10768
조회 수 8486