한국어

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

조회 수 5367
조회 수 5273
FCM 푸시 메세지 전송
admin
2019.09.27
조회 수 5625
조회 수 7420
조회 수 7987
조회 수 6394
조회 수 7230
조회 수 7760
조회 수 7297
조회 수 11184
조회 수 6683
조회 수 7779
조회 수 7070
조회 수 6906
android apk 패키징 v1, v2
admin
2018.12.05
조회 수 7452
조회 수 7159
조회 수 6920
조회 수 7048
조회 수 7990
SDK Platform Release Notes
admin
2018.05.13
조회 수 7349
sdk-tools list
admin
2018.05.13
조회 수 7348
조회 수 7817
조회 수 7739
조회 수 7246
조회 수 7392
Firebase용 Cloud 함수
admin
2018.04.26
조회 수 7758
안드로이드 알람
admin
2018.02.23
조회 수 7937
조회 수 7881
조회 수 7824
gcm 코딩 사례
admin
2018.01.09
조회 수 7693
조회 수 12393
조회 수 8095
조회 수 8748
조회 수 8139
조회 수 10202
FCM PHP Curld
admin
2018.01.01
조회 수 8668
FCM 과 GCM 차이
admin
2018.01.01
조회 수 10278
조회 수 8019