Problems with the differences of an Activity and DialogFragment

2

I have a problem and I can not solve it in any way.

I used to have a Activity called MasInformacion and did the following:

public class MasInformacion extends Activity {

    private TextView editTextNombre ,editTextFecha, editTextZodiaco, editTextEdad, editTextDiasrestantes;
    private Bundle extras;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mas_informacion);

        editTextNombre = (TextView) findViewById(R.id.editTextNombre);
        editTextFecha = (TextView) findViewById(R.id.editTextFecha);
        editTextZodiaco = (TextView) findViewById(R.id.editTextZodiaco);
        editTextEdad = (TextView) findViewById(R.id.editTextEdad);
        editTextDiasrestantes = (TextView) findViewById(R.id.editTextDiasrestantes);

        // Recupera en un Objeto Bundle si tiene valores que fueron pasados como
        // parametro de una actividad.
        extras = getIntent().getExtras();

        if (estadoEditarPersona()) {
            editTextNombre.setText(extras.getString("nombre"));
            editTextFecha.setText(extras.getString("fecha"));
            editTextZodiaco.setText(extras.getString("zodiaco"));
            editTextEdad.setText(extras.getString("edad"));
            editTextDiasrestantes.setText(extras.getString("diasrestantes"));
        }
    }

    /// comprueba el estado de EditarPersona

    public boolean estadoEditarPersona() {
        // Si extras es diferente a null es porque tiene valores. En este caso
        // es porque se quiere editar una persona.
        if (extras != null) {
            return true;
        } else {
            return false;
        }
    }
}

And since MainActivity

public void masInformacion(int p_id){
        Persona persona;
        try{
            persona = baseDatos.getPersona(p_id);
            // Se dirige a la actividad MasInformacion
            Intent actividad_editarPersona = new Intent(this, MasInformacion.class);

            // Carga los datos para mostrar en MasInformacion
            actividad_editarPersona.putExtra("id", p_id);
            actividad_editarPersona.putExtra("nombre", persona.getNombre());
            actividad_editarPersona.putExtra("fecha", persona.getFecha());
            actividad_editarPersona.putExtra("zodiaco", persona.getZodiaco());
            actividad_editarPersona.putExtra("edad", persona.getEdad());
            actividad_editarPersona.putExtra("diasrestantes", persona.getDiasrestantes());
            startActivityForResult(actividad_editarPersona, CODIGO_RESULT_EDITAR_PERSONA);
        }catch (Exception e){
            Toast.makeText(getApplicationContext(), (getResources().getString(R.string.error_mostrarinformacion)), Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }finally{
            baseDatos.cerrar();
        }
    }

Using the method like this:

masInformacion((int)info.id);

And everything worked perfectly. But now I have changed that Activity for a DialogFragment , this:

public class MyDialogFragment extends DialogFragment {

    private TextView editTextNombre ,editTextFecha, editTextZodiaco, editTextEdad, editTextDiasrestantes;
    Bundle extras;

    static MyDialogFragment newInstance() {

        return new MyDialogFragment();
    }

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

        editTextNombre = (TextView) view.findViewById(R.id.editTextNombre);
        editTextFecha = (TextView) view.findViewById(R.id.editTextFecha);
        editTextZodiaco = (TextView) view.findViewById(R.id.editTextZodiaco);
        editTextEdad = (TextView) view.findViewById(R.id.editTextEdad);
        editTextDiasrestantes = (TextView) view.findViewById(R.id.editTextDiasrestantes);


        // Recupera en un Objeto Bundle si tiene valores que fueron pasados como
        // parametro de una actividad.

        Bundle extras = getActivity().getIntent().getExtras();

        if (estadoEditarPersona()) {
            editTextNombre.setText(extras.getString("nombre"));
            editTextFecha.setText(extras.getString("fecha"));
            editTextZodiaco.setText(extras.getString("zodiaco"));
            editTextEdad.setText(extras.getString("edad"));
            editTextDiasrestantes.setText(extras.getString("diasrestantes"));

        }
        return view;
    }

    /// comprueba el estado de EditarPersona

    public boolean estadoEditarPersona() {
        // Si extras es diferente a null es porque tiene valores. En este caso
        // es porque se quiere editar una persona.
        if (extras != null) {
            return true;
        } else {
            return false;
        }
    }
}

And the problem is now, I do not know how to modify MainActivity correctly, I do the following but it does not work, since it does not read the information of my Sqlite :

    void showDialog() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment prev = fm.findFragmentByTag("dialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        masInformacion((int)info.id);


        DialogFragment newFragment = MyDialogFragment.newInstance();
        newFragment.show(ft, "tag");
    }

public void masInformacion(int p_id){
        Persona persona;
        try{
            persona = baseDatos.getPersona(p_id);
            // Se dirige a la actividad MasInformacion
            Intent actividad_editarPersona = new Intent(this, MyDialogFragment.class);

            // Carga los datos para mostrar en MasInformacion
            actividad_editarPersona.putExtra("id", p_id);
            actividad_editarPersona.putExtra("nombre", persona.getNombre());
            actividad_editarPersona.putExtra("fecha", persona.getFecha());
            actividad_editarPersona.putExtra("zodiaco", persona.getZodiaco());
            actividad_editarPersona.putExtra("edad", persona.getEdad());
            actividad_editarPersona.putExtra("diasrestantes", persona.getDiasrestantes());
            startActivityForResult(actividad_editarPersona, CODIGO_RESULT_EDITAR_PERSONA);
        }catch (Exception e){
            Toast.makeText(getApplicationContext(), (getResources().getString(R.string.error_mostrarinformacion)), Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }finally{
            baseDatos.cerrar();
        }
    }

And I use the method like this: showDialog();

The only thing I get this way is to open my DialogFragment but without reading the data of Sqlite and also with this error in logcat

  

android.content.ActivityNotFoundException: Unable to find explicit activity class {anotherintent.dos / anotherinvent.dos.MyDialogFragment}; have you declared this activity in your AndroidManifest.xml?

    
asked by UserNameYo 03.03.2017 в 00:39
source

1 answer

1

Within your AndroidManifest.xml you must declare your Activities:

link

but the problem here is that you try to open a Fragment by means of a Intent as if it were Activity

   Intent actividad_editarPersona = new Intent(this, MyDialogFragment.class);

This can only be done with classes that extend from Activity or AppCompatActivity .

What you have to do is a transaction since your class extends from Fragment .

public class MyDialogFragment extends DialogFragment {

It seems to me that the public void masInformacion(int p_id){ method should actually show Activity MasInformacion therefore only change the class that will open Intent .

        // Se dirige a la actividad MasInformacion
        //Intent actividad_editarPersona = new Intent(this, MyDialogFragment.class);
        Intent actividad_editarPersona = new Intent(this, MasInformacion.class);

Update:

The user wants to open the Fragment, not the Activity, for this a method is created:

public void muestraDialogo(int p_id) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    DialogFragment newFragment = MyDialogFragment.newInstance(p_id, this);
    newFragment.show(ft, "tag");
}

the important part is to initialize the necessary values for this, the method newInstance() of the Fragment is modified, it will receive the context to instantiate the database and obtain the data of the person according to the id:

static MyDialogFragment newInstance(int p_id, Context ctx) {

    MyDialogFragment frag = new MyDialogFragment();
    Bundle bundle = new Bundle();
    mContext = ctx;
    Persona persona;
    baseDatos = new DatabaseHandler(mContext);
    persona = baseDatos.getPersona(p_id);

    bundle.putInt("id", p_id);
    bundle.putString("nombre", persona.getNombre());
    bundle.putString("fecha", persona.getFecha());
    bundle.putString("zodiaco", persona.getZodiaco());
    bundle.putString("ruta_imagen", persona.getRutaImagen());

    frag.setArguments(bundle);
    return frag;
   // return new MyDialogFragment();
}

    
answered by 03.03.2017 / 00:58
source