한국어

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

조회 수 5736
조회 수 5624
FCM 푸시 메세지 전송
admin
2019.09.27
조회 수 5987
조회 수 7799
조회 수 8353
조회 수 6748
조회 수 7594
조회 수 8126
조회 수 7640
조회 수 11543
조회 수 7024
조회 수 8145
조회 수 7430
조회 수 7273
android apk 패키징 v1, v2
admin
2018.12.05
조회 수 7815
조회 수 7523
조회 수 7281
조회 수 7400
조회 수 8344
SDK Platform Release Notes
admin
2018.05.13
조회 수 7697
sdk-tools list
admin
2018.05.13
조회 수 7712
조회 수 8168
조회 수 8108
조회 수 7617
조회 수 7741
Firebase용 Cloud 함수
admin
2018.04.26
조회 수 8121
안드로이드 알람
admin
2018.02.23
조회 수 8283
조회 수 8229
조회 수 8184
gcm 코딩 사례
admin
2018.01.09
조회 수 8053
조회 수 12746
조회 수 8445
조회 수 9093
조회 수 8497
조회 수 10544
FCM PHP Curld
admin
2018.01.01
조회 수 9036
FCM 과 GCM 차이
admin
2018.01.01
조회 수 10628
조회 수 8363