한국어

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

번호
제목
글쓴이
55 안드로이드 스튜디오 PreferenceActivity로 설정창 쉽게 만들기
admin
2020-01-17 24157
54 안드로이드 원그래프 걱은선그래프 만들기 그리기
admin
2019-12-24 23067
53 [Android] activity 인텐트(Intent) 화면 간 이동과 데이터 전달 잘된설명
admin
2019-12-24 22549
52 안드로이드 웹뷰 webview 설명좀 잘해놓은곳 androidx 사용
admin
2019-12-24 23457
안드로이드 비동기 통신 결과값을 Textview set text 값 설정 기록 표시셋 이해 하기 쉽게
admin
2019-12-17 22419
50 안드로이드 6.0 permission 가장잘된 예제
admin
2019-12-12 22051
49 How to fish notification enable
admin
2019-10-13 21932
48 FCM 푸시 메세지 전송
admin
2019-09-27 21686
47 안드로이드 디버깅 Logcat을 이용해서 로그 남기기
admin
2019-09-18 23418
46 FCM 원하는 액티비티 이름을 알림을 요청 전송하여 액티비티 열리게
admin
2019-06-24 24530
45 FirebaseInstanceIdService is deprecated now FCM token
admin
2019-05-29 23655
44 android 9 startForeground requires android.permission.FOREGROUND_SERVICE
admin
2019-05-25 24094
43 Android OS 9 Pie 동작 변경사항 정리 개발자용
admin
2019-05-25 24538
42 Google wants developers to add dark themes to save battery life
admin
2019-03-28 22562
41 Notification에 관한 설명 자세한설명
admin
2019-03-01 25789
40 android.telephony.TelephonyManager.getSubscriberId 베스트코드 code
admin
2018-12-31 24639
39 안드로이드 getDeviceId getImei MEID
admin
2018-12-28 25498
38 안드로이드 백 뒤로가기 버튼 back button of action bar in Android
admin
2018-12-24 33271
37 In favour of overriding onNewToken FirebaseMessagingService
admin
2018-12-22 27816
36 Android Service and Jobscheduler sourcecode
admin
2018-12-22 23162
35 안드로이드 OREO 백그라운드 서비스 제한 대책
admin
2018-12-22 26957
34 JobScheduler - Android Studio Tutorial
admin
2018-12-22 25132
33 Android 9 Pie 새로운기능
admin
2018-12-22 25266
32 동작 변경사항: API 레벨 28+를 대상으로 하는 앱 Android Pie
admin
2018-12-22 27254
31 jobscheduler 간략하게 설명
admin
2018-12-22 23863
30 Oreo 버전 이후 Foreground Service 사용 방법
admin
2018-12-22 29104
29 Android sms intent filter SMS 보내기
admin
2018-12-19 25671
28 add set and cancel of the notification on those listeners
admin
2018-12-14 21890
27 android.os.Build.VERSION_CODES.O 오레오 알림 작성 소스코드
admin
2018-12-14 22859
26 android.os.Build.VERSION_CODES.O 오레오 알림 작성
admin
2018-12-14 24608
25 Android Oreo의 알림 채널
admin
2018-12-10 24897
24 System Permission MODIFY_PHONE_STATE root device
admin
2018-12-08 23729
23 일반 Activity와 AppCompatActivity의 차이 ?
admin
2018-12-06 24573
22 런타임 안드로이드 권한 SDK 23 이상 권한 부여하기 코드
admin
2018-12-06 23497
21 안드로이드 마시맬로 6.0 이상의 런타임 권한
admin
2018-12-05 25391
20 Runtime Permissions in Android Marshmallow 6.0 and above
admin
2018-12-05 30433
19 Android 6.0(API 레벨 23)부터 사용자 런타임에 권한 요청
admin
2018-12-05 24821
18 firebase로 손쉽게 android 앱에 로그인 추가하기
admin
2018-12-05 25986
17 android apk 패키징 v1, v2
admin
2018-12-05 23553
16 Android Shape Drawable Examples
admin
2018-12-05 23361
15 Android Drawable Resources Tutorial
admin
2018-12-05 25342
14 새로운 안드로이드 백그라운드 작업 처리법 : WorkManager
admin
2018-11-25 28374
13 안드로이드 : 컨텐트 제공자 (Content Provider) 연락처 데이터 ContentResolver 객체
admin
2018-11-21 24600
12 Android Contact APP with RecyclerView Part 1: Call Logs Example Android Studio
admin
2018-11-18 25855
11 SMS 또는 통화 기록 사용 위험도 가 높거나 민감한 권한을 사용하는 것을 제한
admin
2018-11-18 27692
10 안드로이드 스튜디오 에서 팩키지명 변경하기 동영상 으로 보기
admin
2018-10-01 24139
9 안드로이드 개발시에 팩키지명 변경하기
admin
2018-09-21 24784
8 안드로이드 전화 수신 발신 이력조회 CALLLOG
admin
2018-09-10 25025
7 Android 6.0 이상 접근권한 checkselfPermission source code
admin
2018-09-06 24529
6 안드로이드 마쉬멜로우 버전 이상에서 권한처리하기.
admin
2018-09-06 23248
5 안드로이드 버전 역사
admin
2018-09-01 24323
4 안드로이드 사용자 주소록리스트 가져오기
admin
2018-06-16 25694
3 @Deprecated @Override 라는 어노테이션 사용 설명
admin
2018-06-16 26633
2 get path dir 함수 종합 정리
admin
2018-05-25 24482
1 unpublish an app in Google Play Developer Console 플레이스토어 앱 게시 삭제
admin
2018-05-21 24782