Close Dialog android

0

I have the following code that correctly shows a Custom listView, however I want that when pressing an Item I close the dialog.

On the internet I have found that it is with builder.dismiss(); but I get an error that this method does not exist.

Thank you very much.

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_segunda_ad);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



        btDialog = findViewById(R.id.btndialog);
        btnDl= findViewById(R.id.imgbtn);

        btnDl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog();
            }
        });

        btDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog();
            }
        });



    }
    public void dialog(){
        final AlertDialog.Builder builder = new AlertDialog.Builder(SegundaAd.this);
        View customView = LayoutInflater.from(SegundaAd.this).inflate(
                R.layout.dilog_layout, null, false);

//        builder.setTitle("Simple Dialog");
//        builder.setMessage("message");
        builder.setView(customView);

        ListView listView = (ListView) customView.findViewById(R.id.dialoglist);

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    this,
                    R.layout.listaitems, listContent);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(SegundaAd.this, ""+listContent[i], Toast.LENGTH_SHORT).show();

                /// Aqui cerrar el Dialog
            }
        });


        builder.setPositiveButton(" ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(SegundaAd.this, "OK", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(SegundaAd.this, "CANCEL", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setIcon(R.drawable.kazdos);

        builder.show();
    }


}
    
asked by Andress Blend 11.11.2017 в 22:32
source

2 answers

0

I have solved it:

I created a variable of type AlertDialog that equals the builder.show and this one if I can use the dismiss();

final  AlertDialog mdialog = builder.show();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(SegundaAd.this, ""+listContent[i], Toast.LENGTH_SHORT).show();

                mdialog.dismiss();

                /// Aqui cerrar el Dialog
            }
        });
    
answered by 12.11.2017 / 02:32
source
0

Try with:

dialog.cancel(); //Cierra dialogo.

It would stay like this:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(SegundaAd.this, ""+listContent[i], Toast.LENGTH_SHORT).show();

                builder.cancel(); //Cierra dialogo.
            }
        });
    
answered by 11.11.2017 в 22:51