私のアプリケーションでは、同じフラグメントのインスタンスが複数あります。個々のフラグメント内で Textview の setText を設定できるようにしたいと考えています。ただし、フラグメントの Textview のテキストを設定しようとすると、すべてのフラグメントでその Textview が変更されます。
各フラグメントに一意のタグまたは ID を使用せずに、フラグメントの個々のインスタンスで Textview を変更するにはどうすればよいですか?
ここに私のフラグメントクラスがあります:
public static class ActivityFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, Bundle savedInstanceState) {
final View fragment1 = inflater.inflate(R.layout.activity_fragment, container, false);
final TextView activityText = (TextView) fragment1.findViewById(R.id.activity_text);
//Calling setText changes the activityText Textview in every fragment onscreen
activityText.setText(text);
return fragment1;
}
}
ここに私の MainActivity クラスがあります:
public class MainActivity extends FragmentActivity {
Static String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i != 5; i++) {
text = "success" + i;
ActivityFragment myFragment = new ActivityFragment();
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, myFragment).commit();
}
}