0

I want pass data from textview fields of my fragment to activity method who insert data to sqlite data base.

This is my fragment with TextView:

    public class BandasFragment extends Fragment {

        private EditText et_codigo, et_nombre, et_genero, et_descripcion;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            // Inflate the layout for this fragment
            View vistaBandas = inflater.inflate(R.layout.fragment_bandas, container, false);

            et_codigo = (EditText)vistaBandas.findViewById(R.id.txt_codigo);
            et_nombre = (EditText)vistaBandas.findViewById(R.id.txt_nombre);
            et_genero = (EditText)vistaBandas.findViewById(R.id.txt_genero);
            et_descripcion = (EditText)vistaBandas.findViewById(R.id.txt_descripcion);



            return vistaBandas;
        }
    }

This is method of my activity who insert data to SQlite database. So i need comunicate from this method to TextView fields of fragment or trasport data from fragment to this method.

public void Registrar(View view){

        AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracion", null, 1);
        SQLiteDatabase BaseDeDatos = admin.getWritableDatabase();

        String codigo =  et_codigo.getText().toString();
        String nombre = et_nombre.getText().toString();
        String genero = et_genero.getText().toString();
        String descripcion = et_descripcion.getText().toString();


        if(!codigo.isEmpty()){
            ContentValues registro = new ContentValues();

            registro.put("codigo", codigo);
            registro.put("nombre", nombre);
            registro.put("genero", genero);
            registro.put("descripcion", descripcion);


            BaseDeDatos.insert("banda", null, registro);

            BaseDeDatos.close();
            et_codigo.setText("");
            et_nombre.setText("");
            et_genero.setText("");
            et_descripcion.setText("");


            Toast.makeText(this,"Registro exitoso", Toast.LENGTH_SHORT).show();
        } else{
            Toast.makeText(this,"Debes llenar todos los campos",Toast.LENGTH_SHORT).show();
        }
    }
Lekr0
  • 653
  • 1
  • 8
  • 17
Oren Diaz
  • 133
  • 7
  • 2
    Possible duplicate of [Passing data between a fragment and its container activity](https://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity) – Vadim Eksler Nov 19 '18 at 13:10
  • You can use [`BroadcastReceiver`](https://developer.android.com/reference/android/content/BroadcastReceiver) , or [`Interface`](https://developer.android.com/training/basics/fragments/communicating#DefineInterface) . and have a look at [This question](https://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity) also. – Rumit Patel Nov 19 '18 at 13:12

2 Answers2

1

We can access data from fragment textview by creating interface which is mediator bw activity and fragment

  1. First you need to create an interface with method.(DataFromFragment.java)

    public interface TestListener { public String listener(String result; }

2.in third step you need to override onAttach Method in Fragment in that method we will get parent activity context. so we can direct get interface method.

   public void onAttach(Context context) {
    super.onAttach(context);
    try {
        mListener = (TestListener) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString() + e.getMessage());
    }
}

3.in 4th step you need implement interface in activity.

public void listener(String result) {
    Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
}
  1. in fifth step you call interface method from fragment method.

    public void returnData() { mListener.listener("Ashok"); }

  2. finally call returndata() method from activity to get result from fragment

    FragmentManager fm = getFragmentManager(); BlankFragment f2 = (BlankFragment) fm.findFragmentById(R.id.fragment); f2.returnData();

Ashok Reddy M
  • 330
  • 3
  • 9
0

If your fragment is under the same activity where you want to send data then just make a new method or update your Registrar method in your activity and your fragment is part of this activity already so just access this method in fragment and pass your value via method. your BandasFragment will be like this:

public class BandasFragment extends Fragment {

private EditText et_codigo, et_nombre, et_genero, et_descripcion;
RegistrarActivity registrarActivity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View vistaBandas = inflater.inflate(R.layout.fragment_bandas, container, false);

    et_codigo = (EditText)vistaBandas.findViewById(R.id.txt_codigo);
    et_nombre = (EditText)vistaBandas.findViewById(R.id.txt_nombre);
    et_genero = (EditText)vistaBandas.findViewById(R.id.txt_genero);
    et_descripcion = (EditText)vistaBandas.findViewById(R.id.txt_descripcion);


    registrarActivity = (RegistrarActivity) getActivity();
    registrarActivity.getData(et_codigo.getText().toString(),
                              et_nombre.getText().toString(), 
                              et_genero.getText().toString(),
                              et_descripcion.getText().toString());

    return vistaBandas;
}

and your class will look like this:

public class RegistrarActivity extends AppCompatActivity {

String codigo,nombre,genero,descripcion;

public void Registrar(View view){

    AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracion", null, 1);
    SQLiteDatabase BaseDeDatos = admin.getWritableDatabase();



    if(!codigo.isEmpty()){
        ContentValues registro = new ContentValues();

        registro.put("codigo", codigo);
        registro.put("nombre", nombre);
        registro.put("genero", genero);
        registro.put("descripcion", descripcion);


        BaseDeDatos.insert("banda", null, registro);

        BaseDeDatos.close();
        et_codigo.setText("");
        et_nombre.setText("");
        et_genero.setText("");
        et_descripcion.setText("");


        Toast.makeText(this,"Registro exitoso", Toast.LENGTH_SHORT).show();
    } else{
        Toast.makeText(this,"Debes llenar todos los campos",Toast.LENGTH_SHORT).show();
    }
}

public void getData(String codigo,String nombre,String genero,String descripcion){
    this.codigo = codigo;
    this.nombre = nombre;
    this.genero = genero;
    this.descripcion = descripcion;
}

}

Taha wakeel
  • 159
  • 1
  • 8