2016-07-18 9 views
-2

私は私のMainActivityAsyncあるクラスを形成する私は「私はMainActivityのものに第二のクラスのContextを設定する必要がありますが、私はドンことを見てきたのTextViewを更新する問題を抱えていますこのシナリオでこれを達成する方法を知っています。私のアプリはこのように見えます。移入のTextView別のクラス

class RestOperation extends AsyncTask<String, Void, Void> { 

    @Override 
    protected Void doInBackground(String... params) { 
    //Java.net Http transaction happens here 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     //I wish to set the value of the TextView with the result here! 
    } 
+0

質問が不明です – Jas

+0

これを確認してください:http://stackoverflow.com/questions/4591878/updating-progress-dialog-in-activity-from-asynctask –

+0

申し訳ありません! OnPostExecuteで返された値を取得し、MAinActivityのTextViewに値(結果)を設定する必要があります。 – joebohen

答えて

0

によってインターフェイスにthisを割り当てますあなたのMainActivityで

//first you have to change the param (the last one) to String so your doInBackground would return String 
class RestOperation extends AsyncTask<String, Void, String> { 
private WeakReference<MainActivity> weakRef; 

public RestOperation(MainActivity activity) { 
    weakRef = new WeakReference<MainActivity>(activity); 
} 

@Override 
protected String doInBackground(String... params) { 
//Java.net Http transaction happens here 
//You need to return some value here to set to the text view later 
return "Something"; 
} 

@Override 
protected void onPostExecute(String result) { 
    MainActivity activity = weakRef.get(); 
    if (activity != null) { 
     //activity is still alive, updates the text view 
     activity.updateText(result); 
    } 
} 

)(それはする必要がないため)onPostExecuteは実行されませんし、AsyncTaskは、メモリリークが発生しません。

new RestOperation(this).execute("Your input param"); 

もMainActivity内の別の方法で行います。

public void updateText(String result) { 
    textView.setText(result); 
} 
+0

これは、 'new RestOperation(this).execute("入力パラメータ ");これは赤で下線が引かれています:RestOperationのRestOperation(mainActivity)は適用できません。 – joebohen

+0

ああ、操作を呼び出す正しい方法を発見しました。 新しいRestOperation(MainActivity.this).execute( "あなたの入力パラメータ"); ありがとうございます。 – joebohen

+0

@joebohenようこそ。 –

0

は単純に使用します。そして、あなたがメインのアイテムにアクセスすることができ

class RestOperation extends AsyncTask<String, Void, Void> { 

Context context; 
add public RestOperation (Contex cntx) { 
this.context = cntx; 
} 
    @Override 
    protected Void doInBackground(String... params) { //Java.net Http transaction happens here } 

@Override 
    protected void onPostExecute(Void result) { 
     //I wish to set the value of the TextView with the result here! 
    } 

そして、あなたの猫このタスク

new ResrOperation(getApplicationContext()).excute(); 

@Override 
protected void onPostExecute(String result) 
    { 
     textView.setText(result); 
    } 
+0

私はAndroidに新しいので、私と一緒にご負担ください! RestOperationは別のクラスであるため、OnPostExecuteではtextviewを使用できません。 TextViewへの参照を追加しようとすると、findViewByIdを解決できません。 – joebohen

0
はにあなたのコードを変更

アクティビティ

あなたはあなたのメインactiviryのメソッドをonPostExecuteオーバライドすることができ、その後、あなたは活動へのアクセスもがあまりにもelemetsあります

もう一品

を(この場合は何のコンテキストがneddedない)

new ResrOperation(){ 
@Overide 
void onPostExecute(Long result) { 
    // access elemets from activity 
} 
}.excute(); 
1

あなたは次のようにそれを行うことができますこの:

#1:あなたのからContextを得るためにあなたのRestOperationクラスのコンストラクタを作ります

public RestOperation(MainActivity activityContext){ 
     this._activityContext = activityContext; 
    } 

#2

class RestOperation extends AsyncTask<String, Void, String> { 
    MainActivity _activityContext; 
    public RestOperation(MainActivity activityContext){ 
     this._activityContext = activityContext; 
    } 
    @Override 
    protected String doInBackground(String... params) { 
     //Java.net Http transaction happens here 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     ((TextView)_activityContext.findViewById(R.id.yourTextViewId)).setText(yourText); 
    } 
} 
:だから

new RestOperation(MainActivity.this).execute("yours","params","here"); 

、あなたAsyncTaskクラスはこのようになります。このようなAsyncTaskクラスが初期化中Activityコンテキストを渡し、このよう0

+0

「setText」メソッドを解決できません! – joebohen

0

Asyncクラスに最初にinterfaceと書きます

CompleteListener completeListener; 

public void setListener(CompleteListener cmpltListnr) { 
    this.completeListener = cmpltListnr; 
} 

public interface CompleteListener { 
    void OnCompleteListener(String result); 
} 

@Override 
protected void onPostExecute(Void result) { 
    if (completeListener != null) { 
     completeListener.OnCompleteListener(""+result); 
    } 
} 

onPostExecuteimplementあなたが望むActivityでリスナー。これは別のクラスである場合は、コンストラクタ(アクティビティが破棄されるとなるように、私はここにWeakReferenceを使用することに注意してMainActivityへの参照を取得する必要があります機能 をオーバーライドし、

yourAsynClassObject.setListener(this); 

@Override 
public void OnCompleteListener(String result) { 
    textview.setText("" + result); 
} 
+0

主なアクティビティRestOperation.setListener(リスナー)からリスナーを設定する方法が不明です。私は静的なコンテキストから参照することはできません! – joebohen

+0

@joebohen 'RestOperation'のオブジェクトを作成してから' setListener() 'を呼び出します。 – Sanoop

1
  1. あなたはonPostExecuteで呼び出すと、メインの活動でそのインターフェイスを実装しますインターフェイスを作成し、値を取得する場合、それはいいだろうとし、値をtextviewで設定します。

2.newrestOperation(YourActivity.this、textviewobject).execute( "あなた"、 "のparams"、 "ここ");

だから、あなたのAsyncTaskクラスは次のようになります。

Textview mtxt; 
class RestOperation extends AsyncTask<String, Void, Void> { 
    Context _activityContext; 
    public RestOperation(Context activityContext,Textview txt){ 
     this._activityContext = activityContext; 
     this.mtxt = txt; 
    } 
    @Override 
    protected String doInBackground(String... params) { 
     //Java.net Http transaction happens here 
    } 

    @Override 
    protected void onPostExecute(String result) {enter code here 
     mtxt .setText(yourText); 
    } 
} 

しかし、#1は、より良い方法です。

関連する問題