2016-08-15 6 views
0

Fragment_BのテキストをFragment_Aから変更したいとします。これどうやってするの?他のアクティビティ/フラグメントのテキストを設定する方法

このようなものです。

Fragment_A

ola = (TextView) v.findViewById(R.id.textView12); 
ola2 = (TextView) (FRAGMENT_B).findViewById(R.id.textView2); 

AsyncTask(ola,ola2).execute //this should set text on Fragment_A and Fragment_B 

フラグメントA:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.activity_second,container,false); 


    ola = (TextView) v.findViewById(R.id.textView12); 

    Asynctask(ola).execute 

    return v; 
} 

断片B:

public class Fragmento_B extends android.support.v4.app.Fragment{ 

    public TextView ola2; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.activity_third,container,false); 

     ola2 = (TextView) v.findViewById(R.id.textView2); 

     Asynctask(ola2).execute 
     return v; 
    } 
} 
+0

フラグメントBを引数で開始し、フラグメントBで引数をフェッチしてから、引数から値をフェッチし、値からsetTextをフェッチします。 –

答えて

0

あなたが直面している問題は何ですか?何らかのエラーが表示されているのですか、テキストをあなたのものとして変更していないのでしょうか?

+0

私のコードは正常に動作していますが、AsyncTask.executeを2回呼び出しています。私はそれを一度実行し、fragment_Aからfragment_Bテキストビューを更新したいだけです。このようにして、fragment_BでAsyncTask.executeを実行する必要はありません。 –

+0

私は、フラグメントAにola2の参照を渡すだけで、ola2ビューでsetTextメソッドを呼び出す必要があると思います。私はAsyncTaskが必要と思わない –

関連する問題