Java – settext from another fragment independent activity
•
Java
I want to set textview in the fragment of another activity. This activity is not a fragment transaction of mainactivity
I have tried some methods in other related articles related to my problem, but something went wrong
This is my method in the clip to receive another activity
Segment a
public class FragmentA extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProgressDialog pDialog = new ProgressDialog(getContext());
pDialog.setCancelable(false);
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// Inflating view layout
View layout = inflater.inflate(R.layout.fragment_A,container,false);
//Put Data to id fragment
valueName = (TextView) layout.findViewById(R.id.valueNameNav);
valueStatus = (TextView) layout.findViewById(R.id.valueStatusNav);
}
public void setText(String name,String status){
valueName = (TextView) getView().findViewById(R.id.valueNameNav);
valueName.setText(name);
valueStatus = (TextView) getView().findViewById(R.id.valueStatusNav);
valueStatus.setText(status);
}
}
This is how I invoke the setText method in the fragment from the activity.
String editValueName= editName.getText().toString();
String lastStatus = valueStatus.getText().toString();
FragmentA mFragment = (FragmentA )
getSupportFragmentManager().findFragmentById(R.id.fragment_A);
mFragment.setText(editValueName,lastStatus);
But got such a mistake
100% confirm that there is a data string on the string gettext
Solution
Create a FrameLayout in the activity with the ID container height match_ PARENT
FragmentA newFragment = new FragmentA ();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container,newFragment).commit();
Instead of setting your text
String editValueName= editName.getText().toString(); String lastStatus = valueStatus.getText().toString(); newFragment .setText(editValueName,lastStatus);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
