Problem going from one fragment to another using FragmentTransaction

0

Greetings, you will see I am new in this forum as well as in androi. A couple of days ago I have been doing a small application based on a tabbed activity which has four tabs (SETUP, STATUS, CHARTS AND SETTINGS) based on a fragment and the elements of each tab is contained in a Relative layout. The problem I have is that I can not go from one fragment to another from an alertDialog. So far this is the code I'm using.

    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_container, goSettings);
        fragmentTransaction.commit();
    }
} );
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();*/

    }
} );
alertSettings.show();

Someone who can help me with this problem please, thank you in advance.

    
asked by Cesar Herbas 14.01.2018 в 17:19
source

1 answer

0

Well this is the fragment code

public class SetupFragment extends Fragment{

EditText editName,editMake,editModel,editSerial,editDate,editCapacity,editCA,editCCA;
Button loadButton,saveButton;
ConnectionSQLiteHelper connectorSetup;
@Override
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);
    connectorSetup = new ConnectionSQLiteHelper(getActivity(),"db_batmon",null,1);
    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()){

                        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();
                        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_container, 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");
            }
        }
    });
}

}

    
answered by 15.01.2018 в 00:59