Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
Clickable Options in AlertDialog after ListView long click

I am trying to set up a listView with an OnItemLongClickListener(), that will open up a alert dialog with a custom layout. I then want the layout to have two buttons, one to open up another activity to edit the entry in the SQLite DB, and one to delete the entry. My onItemLongClickListener() works, and I can open a custom layout, but I cant figure out how to make the dialog close after I click the button, or execute the methods I want it to.

My ListViewItemLongClick():

private void listViewItemLongClick(){
    final ListView myList = (ListView) findViewById(R.id.resourcesList);
    myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
             Id = id;


            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
            // ...Irrelevant code for customizing the buttons and title
            LayoutInflater inflater = getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.alert_layout, null);
            dialogBuilder.setView(dialogView);

            AlertDialog alertDialog = dialogBuilder.create();
            alertDialog.show();



            return true;
        }
    });

}

My custom alert layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/Edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="edit"
        android:text="Edit" />

    <Button
        android:id="@+id/delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="delete"
        android:text="Delete" />
</LinearLayout>

Any help would be much appreciated, Im still very new to android programming.

Cheers!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

With my solution, you don't need an XML file. Of course you can use it for add an EditText for exemple and get it with an EditText ed = (EditText)dialog.findViewById(R.id.dialog_get_name_ed);

But for your case, Try this

    dialogBuilder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // you have your edit button
        }
    });

    dialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // you have your delete button
        }
    });
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda