0

I have this method in my MainActivity and I want to use both in a button on a fragment but i don't know how to send the data from EditText in the fragment to the activity and use the buttons.

public void alta(View v) {
        AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this);
        SQLiteDatabase bd = admin.getWritableDatabase();
        String nome = et1.getText().toString();
        String edad = et2.getText().toString();
        String geno = et3.getText().toString();
        String curp = et4.getText().toString();
        ContentValues registro = new ContentValues();
        registro.put("nombre", nome);
        registro.put("edad", edad);
        registro.put("genero", geno);
        registro.put("curp", curp);
        bd.insert("usuarios", null, registro);
        bd.close();
        et1.setText("");
        et2.setText("");
        et3.setText("");
        et4.setText("");
        Toast.makeText(this, "Se cargaron los datos",
                Toast.LENGTH_SHORT).show();
    }

    public void modificacion(View v) {
        AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this);
        SQLiteDatabase bd = admin.getWritableDatabase();
        String nome = et1.getText().toString();
        String edad = et2.getText().toString();
        String geno = et3.getText().toString();
        String curp = et4.getText().toString();
        ContentValues registro = new ContentValues();
        registro.put("nombre", nome);
        registro.put("edad", edad);
        registro.put("genero", geno);
        registro.put("curp", curp);
        int cant = bd.update("usuarios", registro, "curp=" + curp, null);
        bd.close();
        if (cant == 1)
            Toast.makeText(this, "se modificaron los datos", Toast.LENGTH_SHORT)
                    .show();
        else
            Toast.makeText(this, "no existe un usuario con ese nombre",
                    Toast.LENGTH_SHORT).show();
    }

I have this in fragment class but I don't know how to pass the EditTexts values.

public class Fragment1 extends Fragment {

    EditText iet1,iet2,iet3,iet4;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        iet1 = (EditText) getActivity().findViewById(R.id.et1);
        iet2 = (EditText) getActivity().findViewById(R.id.et2);
        iet3 = (EditText) getActivity().findViewById(R.id.et3);
        iet4 = (EditText) getActivity().findViewById(R.id.et4);
    }

    public Fragment1() {
    }

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

        return rootView;
    }
}
Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
  • I didn't work with sqlite but for sending data from fragment to an activity you can create a interface with a method to share data, implement that interface inside your activity and instantiate that interface from onViewCreate inside your fragment and use that method to send data here a good tutorial https://www.youtube.com/watch?v=VyyGP_d0Ia8 – Abdennacer Lachiheb Nov 17 '16 at 21:33
  • Try to Use Intent Check [This](http://stackoverflow.com/a/7325248/6747577) – Malik Abu Qaoud Nov 17 '16 at 23:14
  • Use interface to communicate from fragment to activity – Rohit Singh Feb 06 '19 at 20:55

1 Answers1

0

First, it is important that the Activity will not try to access the views inside of the fragment directly (this will work initially, but in edge cases would cause crashes due to invalid references). What I suggest instead is create an interface in the MainActivity that would the Fragment to notify the Activity.

public class MainActivity {
     String textArr[4] = new String[4];
     public void setEditTextValue(String text, int id) {
         textArr[id] = text;
     }
}

public class Fragment1 extends Fragment {

   public void updateData() {
       MainActivity main = (MainActivity)getActivity();
       main.setEditTextValue(iet1.getText().toString(), 0);
       main.setEditTextValue(iet2.getText().toString(), 1);
       main.setEditTextValue(iet3.getText().toString(), 2);
       main.setEditTextValue(iet4.getText().toString(), 3);
       main.modificacion(); // Which doesn't need view and can just use the data saved in the array
   }
}
Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
  • This a bad solution, you have to decouple the fragment from the activity using an interface so you can reuse the fragment with an other activity. – Abdennacer Lachiheb Nov 17 '16 at 21:58
  • @Abdenaceur, you can obviously do what you are suggesting, however, sometimes (and I think, even most times), a Fragment can have only one Activity and then you don't need to generalize things and you can keep it simple stupid. So "bad" and "good" depend on your specific needs. – Doron Yakovlev Golani Nov 18 '16 at 14:05