Get Fragment data in a ViewPager (Form)

0

I have a Activity that contains 5 Fragment with a ViewPager type form, what I want is to recover the information of each EditText of the Fragment as it happens with the swipe from one fragment to another, I do not know how to implement it, in principle I know it is with Bundle but I do not know how to activate it when changing fragment.

This is the code of my Activity that handles the ViewPage :

public class Agregar_Expediente extends AppCompatActivity implements EstacionFragment.OnFragmentInteractionListener, UCentralFragment.OnFragmentInteractionListener,
    MonitorFragment.OnFragmentInteractionListener, TecladoFragment.OnFragmentInteractionListener, BocinasFragment.OnFragmentInteractionListener,
    MouseFragment.OnFragmentInteractionListener, UpsFragment.OnFragmentInteractionListener, ImpresoraFragment.OnFragmentInteractionListener,
    ScannerFragment.OnFragmentInteractionListener

{

private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_agregar__expediente);

    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarAgregar);
   // setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_agregar__expediente, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onFragmentInteraction(Uri uri) {

}

public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Define cual pantalla va a mostrar
     */
    public static Fragment newInstance(int sectionNumber) {
        /*PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);*/

        Fragment fragment = null;

        switch (sectionNumber){
            case 1: fragment = new EstacionFragment();
                break;
            case 2: fragment = new UCentralFragment();
                break;
            case 3: fragment = new MonitorFragment();
                break;
            case 4: fragment = new BocinasFragment();
                break;
            case 5: fragment = new TecladoFragment();
                break;
            case 6: fragment = new MouseFragment();
                break;
            case 7: fragment = new UpsFragment();
                break;
            case 8: fragment = new ImpresoraFragment();
                break;
            case 9: fragment = new ScannerFragment();
        }


        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_agregar__expediente, container, false);



        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 9;
    }
}

}

and this is one of my Fragments :

public class UCentralFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

ScrollView scrollUCentral;

TextInputEditText txt_numInvUCentral, txt_marcaUCentral,
        txt_modeloUCentral,txt_tipoUCentral, txt_detallesUCentral, txt_selloUCentral;

TextInputLayout imp_numInvUCentral, imp_marcaUCentral,
        imp_modeloUCentral,imp_tipoUCentral, imp_detallesUCentral, imp_selloUCentral;

private OnFragmentInteractionListener mListener;

public UCentralFragment() {
    // Required empty public constructor
}

@Override
public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
    super.startActivityForResult(intent, requestCode, options);

}

public static UCentralFragment newInstance(String param1, String param2) {
    UCentralFragment fragment = new UCentralFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_ucentral, container, false);
    iniciarVariables(view);


    return view;
}

private void iniciarVariables(View view){
    txt_numInvUCentral = (TextInputEditText) view.findViewById(R.id.txt_numInvUCentral);
    txt_marcaUCentral = (TextInputEditText) view.findViewById(R.id.txt_marcaUCentral);
    txt_modeloUCentral = (TextInputEditText) view.findViewById(R.id.txt_modeloUCentral);
    txt_tipoUCentral = (TextInputEditText) view.findViewById(R.id.txt_tipoUCentral);
    txt_detallesUCentral = (TextInputEditText) view.findViewById(R.id.txt_detallesUCentral);
    txt_selloUCentral = (TextInputEditText) view.findViewById(R.id.txt_selloUCentral);


    imp_numInvUCentral = (TextInputLayout) view.findViewById(R.id.imp_numInvUCentral);
    imp_marcaUCentral = (TextInputLayout) view.findViewById(R.id.imp_marcaUCentral);
    imp_modeloUCentral = (TextInputLayout) view.findViewById(R.id.imp_modeloUCentral);
    imp_tipoUCentral = (TextInputLayout) view.findViewById(R.id.imp_tipoUCentral);
    imp_detallesUCentral = (TextInputLayout) view.findViewById(R.id.imp_detallesUCentral);
    imp_selloUCentral = (TextInputLayout) view.findViewById(R.id.imp_selloUCentral);


}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

@Override
public void onDestroy() {
    super.onDestroy();

    Bundle bundle = new Bundle();
    bundle.putStringArrayList("vUCentral", valoresInput());
}

private ArrayList<String> valoresInput(){
    ArrayList<String> arrayInput = new ArrayList<>();

    arrayInput.add(0, txt_numInvUCentral.getText().toString());
    arrayInput.add(1, txt_marcaUCentral.getText().toString());
    arrayInput.add(2, txt_modeloUCentral.getText().toString());
    arrayInput.add(3, txt_tipoUCentral.getText().toString());
    arrayInput.add(4, txt_detallesUCentral.getText().toString());

    return arrayInput;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

}

What I want is to send an automatic Bundle when changing fragment, I do not know if I make myself understand

    
asked by Yeikel200 17.04.2018 в 00:13
source

0 answers