Greetings ... I tell you that I am working on an app based on a tabbed activity, in which I have four tabs (setup, status, charts and settings) where each tab consists of a fragment. What I try to do is to move from the tab setup to settings or status, according to an option that is chosen in an AlertDialog. The code that I could find is the following, but it does not work for me.
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
SettingsFragment goSettings = new SettingsFragment();
fragmentTransaction.replace(R.id.fragment_settings, goSettings);
fragmentTransaction.commit();
The problem seems to be in R.id.fragment_settings, since this is not the correct layout settings, but how can I know which is the correct container? Someone who can help me with this little problem please. I leave the complete code of the fragment setup. Thanks in advance
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_setup, container, false);
editName = (EditText) v.findViewById(R.id.editSetup1);
editMake = (EditText) v.findViewById(R.id.editSetup2);
editModel = (EditText)v.findViewById(R.id.editSetup3);
editSerial = (EditText) v.findViewById(R.id.editSetup4);
editDate = (EditText) v.findViewById(R.id.editSetup5);
editCapacity = (EditText) v.findViewById(R.id.editSetup6);
editCA = (EditText) v.findViewById(R.id.editSetup7);
editCCA = (EditText) v.findViewById(R.id.editSetup8);
loadButton = (Button) v.findViewById(R.id.bSetupLoad);
saveButton = (Button) v.findViewById(R.id.bSetupSave);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getActivity().getApplicationContext(),"Saving...",Toast.LENGTH_LONG).show();
final AlertDialog.Builder alertSave = new AlertDialog.Builder(getActivity());
alertSave.setCancelable(false);
alertSave.setMessage("Do you want to save the battery data?");
alertSave.setTitle("Save Data");
alertSave.setPositiveButton( "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String nameBat = editName.getText().toString();
String makeBat = editMake.getText().toString();
String modelBat = editModel.getText().toString();
String serialBat = editSerial.getText().toString();
String dateBat = editDate.getText().toString();
String capacityBat = editCapacity.getText().toString();
String caBat = editCA.getText().toString();
String ccaBat = editCCA.getText().toString();
if(!nameBat.isEmpty()&&!makeBat.isEmpty()&&!modelBat.isEmpty()&&!serialBat.isEmpty()&&!dateBat.isEmpty()&&!capacityBat.isEmpty()&&!caBat.isEmpty()&&!ccaBat.isEmpty()){
ConnectionSQLiteHelper connectorSetup = new ConnectionSQLiteHelper(getActivity(),"db_batmon",null,1);
SQLiteDatabase dbBat = connectorSetup.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Utilities.FIELD_NAME,nameBat);
values.put(Utilities.FIELD_MAKE,makeBat);
values.put(Utilities.FIELD_MODEL,modelBat);
values.put(Utilities.FIELD_SERIAL,serialBat);
values.put(Utilities.FIELD_DATE,dateBat);
values.put(Utilities.FIELD_CAPACITY,capacityBat);
values.put(Utilities.FIELD_CA,caBat);
values.put(Utilities.FIELD_CCA,ccaBat);
Long idResult = dbBat.insert(Utilities.TABLE_BATTERY,Utilities.FIELD_ID,values);
Toast.makeText(getActivity().getApplicationContext(),"ID Register: "+idResult,Toast.LENGTH_LONG).show();
dbBat.close();
connectorSetup.close();
AlertDialog.Builder alertSettings =new AlertDialog.Builder(getActivity());
alertSettings.setCancelable(false);
alertSettings.setMessage("Do you want to make some configuration for data sampling?");
alertSettings.setTitle("Configure Sampling");
alertSettings.setPositiveButton( "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
SettingsFragment goSettings = new SettingsFragment();
fragmentTransaction.replace(R.id.fragment_settings, goSettings);
fragmentTransaction.commit();
Toast.makeText(getActivity().getApplicationContext(),"Opening Settings...",Toast.LENGTH_LONG).show();
}
} );
alertSettings.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/*FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
StatusFragment goStatus = new StatusFragment();
fragmentTransaction.replace(R.id.fragment_container, goStatus);
fragmentTransaction.commit();*/
Toast.makeText(getActivity().getApplicationContext(),"Opening Status...",Toast.LENGTH_LONG).show();
}
} );
alertSettings.show();
}else{
Toast.makeText(getActivity().getApplicationContext(),"Complete all fields please",Toast.LENGTH_LONG).show();
}
}
} );
alertSave.setNegativeButton("Cancel", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity().getApplicationContext(),"Cancelled",Toast.LENGTH_SHORT).show();
}
} );
alertSave.show();
}
});
return v;
}
@Override
public void onStart() {
super.onStart();
editDate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
DateDialog dialog = new DateDialog(v);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
dialog.show(fragmentTransaction,"DatePicker");
}
}
});
}