Assigning values to the views of an AlertDialog

0

I created a custom layout for my AlertDialog with a TextView in it, and an image, but I can not put values in it. Does anyone know how to solve it?

Follow the code:

private void carregaAlert(){
    View v = getLayoutInflater().inflate(R.layout.alerta, null);

    AlertDialog.Builder dialog = new AlertDialog.Builder(ClassificacaoActivity.this);

    dialog.setView(v).setPositiveButton("SIM", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            Intent vai = new Intent(ClassificacaoActivity.this, InteraActivity.class);
            startActivity(vai);
        }
    }).setNegativeButton("NÃO", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // Continua na tela
        }
    });
    ImageView img = (ImageView) findViewById(R.id.ima);
    img.setImageResource(R.drawable.trofeu);
    dialog.show();
}
10-13 21:50:52.280 844-844/? I/art: Not late-enabling -Xcheck:jni (already on)
10-13 21:50:52.280 844-844/? W/art: Unexpected CPU variant for X86 using defaults: x86
10-13 21:50:52.482 844-844/br.com.franciscocartaxo.aps2 W/System: ClassLoader referenced unknown path: /data/app/br.com.franciscocartaxo.aps2-1/lib/x86
10-13 21:50:52.502 844-844/br.com.franciscocartaxo.aps2 I/InstantRun: starting instant run server: is main process
10-13 21:50:52.644 844-844/br.com.franciscocartaxo.aps2 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
10-13 21:50:52.884 844-874/br.com.franciscocartaxo.aps2 I/OpenGLRenderer: Initialized EGL, version 1.4
10-13 21:50:52.885 844-874/br.com.franciscocartaxo.aps2 D/OpenGLRenderer: Swap behavior 1
10-13 21:50:52.893 844-874/br.com.franciscocartaxo.aps2 D/EGL_emulation: eglCreateContext: 0xa2817a60: maj 2 min 0 rcv 2
10-13 21:50:52.912 844-874/br.com.franciscocartaxo.aps2 D/EGL_emulation: eglMakeCurrent: 0xa2817a60: ver 2 0 (tinfo 0xa2854330)
10-13 21:50:52.938 844-874/br.com.franciscocartaxo.aps2 D/EGL_emulation: eglMakeCurrent: 0xa2817a60: ver 2 0 (tinfo 0xa2854330)
10-13 21:50:56.047 844-844/br.com.franciscocartaxo.aps2 W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
10-13 21:50:56.056 844-874/br.com.franciscocartaxo.aps2 D/EGL_emulation: eglMakeCurrent: 0xa2817a60: ver 2 0 (tinfo 0xa2854330)
    
asked by anonymous 14.10.2017 / 03:16

2 answers

1

To get references to AlertDialog layout views, you have to use the findViewById() method of this layout instead of the Activity method.

Instead of

ImageView img = (ImageView) findViewById(R.id.ima);

use

ImageView img = (ImageView) v.findViewById(R.id.ima);
    
14.10.2017 / 22:08
0

Francisco, at these times I usually create DialogFragment, which follow the principles of common fragments, getting layout.xml and everything else, and the way it is implemented is quite simple, for example, to call a FragmentDialog in its function loadAlert ():

DialogoFragmento fragmentCardapio = new DialogoFragmento ();
            fragmentCardapio.show(MainActivity.this.getSupportFragmentManager(), "Chamar Dialog fRAGMENT");

This will call the fragment you created that extends the DialogFragment class:

public class EventoDialogFragment extends DialogFragment implements View.OnClickListener{

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_evento, container, false);
    this.getDialog().setTitle("Dialog chamado pela Main");
   //Daqui para frente você pode inserir o código como se estivesse trabalhando com um fragment normal

    return view;
}
}

You'll see that the XML file will bother to build the object the exact size of the layout, so you can get things right! I hope I have helped!

    
14.10.2017 / 14:28