2017-08-04 33 views
1

最初のフラグメントのEditText入力を、ボタンクリックの2番目のフラグメントのテキストビューに渡したいとします。しかし、textviewは、ボタンクリックがトリガされたときに "null"値を表示します。フラグメントからフラグメントへEditText

最初のフラグメント:

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

    TextView tv = (TextView) v.findViewById(R.id.tvFragFirst); 
    tv.setText(getArguments().getString("msg")); 

    Button btn = (Button) v.findViewById(R.id.tryBtn); 
    btn.setOnClickListener(new View.OnClickListener() { 
           @Override 
           public void onClick(View v) { 
            EditText woo = (EditText)v.findViewById(R.id.editText); 
            MainActivity.myBundle.putString("Try", String.valueOf(woo)); 
            switch (v.getId()) { 
             case R.id.tryBtn: 
             SecondFragment home = new SecondFragment(); 
              FragmentTransaction ft = getFragmentManager().beginTransaction(); 
              ft.replace(R.id.first_frag, home); 
              ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK); 
              ft.addToBackStack(null); 
              ft.commit(); 
              break; 
            } 
           } 
          } 
    ); 

    return v; 
} 

第2のフラグメント:

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

    TextView tv = (TextView) v.findViewById(R.id.tvFragSecond); 
    TextView tvTry = (TextView) v.findViewById(R.id.tvTry); 
    String myValue = (String) MainActivity.myBundle.get("Try"); 

    return v; 
} 

MainActivity:そのため

public static Bundle myBundle = new Bundle(); 

答えて

0

最初のフラグメント

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

//  TextView tv = (TextView) v.findViewById(R.id.tvFragFirst); 
//  tv.setText(getArguments().getString("msg")); 


     final EditText woo = (EditText)v.findViewById(R.id.editText); 

     Button btn = (Button) v.findViewById(R.id.tryBtn); 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       SecondFragment home = new SecondFragment(); 

       Bundle bundle = new Bundle(); 
       bundle.putString("Try", woo.getText().toString()); 
       home.setArguments(bundle); 

       FragmentTransaction ft = getFragmentManager().beginTransaction(); 
       ft.replace(R.id.first_frag, home); 
       ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK); 
       ft.addToBackStack(null); 
       ft.commit(); 

      } 
     }); 

     return v; 
    } 

第2のフラグメント

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

    TextView tv = (TextView) v.findViewById(R.id.tvFragSecond); 
    TextView tvTry = (TextView) v.findViewById(R.id.tvTry); 

    Bundle arguments = getArguments(); 
    if (arguments != null) { 
     String myValue = arguments.getString("Try"); 
     tvTry.setText(myValue); 
    } 


    return v; 
} 

とあなたのMainActivityにこの行を削除し、このコードを試してみてください、それは静的なバンドルを使用していない方が良いでしょう。

public static Bundle myBundle = new Bundle(); 
+0

ありがとうございました。これは完全に機能しました..ボタンクリックで複数の値を追加するのはどうですか? – randolfrojo11

+0

作成したバンドルに入れてください: bundle.putString( "secondTry"、woo2.getText()。toString());それから引数から取得します。String myValue2 = arguments.getString( "secondTry"); –

0

使用バンドル、コードの下のショー。

Fragment fragment = new SecondFragment(); 
    FragmentManager fm =getSupportFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 
    ft.replace(R.id.frame_layout, fragment); 
    ft.commit(); 

    Bundle bundle = new Bundle(); 
    bundle.putString("id", woo.getText().toString()); 
    fragment.setArguments(bundle); 

この値はSecondFragmentのようになります。

Bundle bundle = this.getArguments(); 
     if (bundle != null) { 
     String id = bundle.getString("id"); 
     } 
tv.setText(id); 
1

パラメータについてはnewInstanceを呼び出してください。あなたの声明について

SecondFragment.newInstance(/*your text*/) //when you open your fragment 

//in your Second Fragment 
public static SecondFragment newInstance(String data) { 
    SecondFragment sFragment = new SecondFragment(); 
    Bundle bundle = new Bundle(); 
    bundle.putString("data", data); 
    sFragment.setArguments(bundle); 
    return sFragment; 
    } 
+0

この行を追加するのを忘れた:bundle.putString( "Try"、woo.getText()。toString());あなたのコードで。 @HareshChhelana右。 –

+0

ありがとう=) –

+0

すみません。しかし、SecondFragment.newInstance(/ * your text * /)をどこに置くべきか、そして "あなたのテキスト"の中に入れたいものはどこに置かなければならないのですか?SecondFragment newInstance(String data){ SecondFragment sFragment = new SecondFragment() ; バンドルバンドル= newバンドル(); bundle.putString( "data"、data); sFragment.setArguments(bundle); return sFragment; } ? 大変申し訳ございません。私はAndroid開発の初心者です。 – randolfrojo11

0

しかし、ボタンのクリックがトリガされたときにTextViewには "ヌル" 値が表示されます。それは使用しています私はEventBus

を使用した断片間で情報を渡すことについて

woo.getText().toString() 

この

EditText woo = (EditText)v.findViewById(R.id.editText); 
String.valueOf(woo) 

これを試してみてくださいEDITTEXTからテキストを取得する方法はありませんオブザーバーパターン:

あなたがイベントをサブスクライブ:

@Subscribe(threadMode = ThreadMode.MAIN) 
public void onMessageEvent(MessageEvent event) {/* Do something */}; 

あなたがイベントをポスト:あなたは同時に両方の断片を持っている場合は動作します

EventBus.getDefault().post(new MessageEvent()); 

。 あなたのロジックを考えると、両方の断片がありません。だから私は、次の操作を行います:正しい情報とフラグメントを変更するための責任を負うことになります

mainActivity.onSentInputField(woo.getText().toString()); 

そして、あなたの活動:

あなたのフラグメントは次のようにあなたの活動を呼び出す必要があります。この方法でコードを整理し、フラグメントを1か所で変更します。

+0

これは正しい方法だと思いますか? 2つのフラグメント間でデータを渡しますか? –

+0

@HareshChhelana答え更新 –

関連する問題