Assign values to a Alertdialog views

Asked

Viewed 131 times

0

I created a custom layout for my Alertdialog with a Textview in it, and an image, but I can’t put values in it. Does anyone know how to solve?

Follows 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)
  • What exactly is going on? What was expected? You logged in but there’s nothing related to your problem in it.

2 answers

2


To get references to Alertdialog layout views you have to use the method findViewById() of this layout and not of the Activity.

Instead of

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

use

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

0

Francisco, at these times I normally create Dialogfragment, which follow the principles of common Fragments, receiving layout.xml and everything else and the form of deployment is very simple, for example, to call a Fragmentdialog in its function to load():

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 will see that the XML file worries about building the object of the exact layout size, so you can set things right! I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.