最初に、以下のようにインフレータをonCreateView(2ndFragmentClass内)に宣言します。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content, container, false);
return view;
}
Fragment_contentは、少なくともそれ自体の中にTextViewを持っている必要があることを考慮してください(フラグメント内にその値を設定するため)。次に、このテキストの値を最初のフラグメントから変更する必要があります。したがって、このコンストラクターを2番目のフラグメント(TextViewを含むフラグメント)内に次のように追加します。
public void setText(String name) {
TextView txt= (TextView ) getView().findViewById(R.id.textView1);
txt.setNewText(name);
}
簡単に言うと、次のようになります。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content, container, false);
return view;
}
public void setText(String name) {
TextView txt= (TextView ) getView().findViewById(R.id.textView1);
txt.setNewText(name);
}
次に、1stFragmentClassから2ndFragmentに設定する必要のあるテキストを定義する必要があります。次に、次のように1番目のフラグメントのボタンを押して2番目のフラグメントのテキストを設定します。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String url = "Hello, This is the text from 1st Fragment:)";
//Here we try to declare 2nd fragment.
2ndFragmentClass fragment = (2ndFragmentClass) getFragmentManager()
.findFragmentById(R.id.detailFragment);
fragment.setNewText(url);
}