Question

I´ve got a ListView that I give an OnItemLongClickListener. On LongClick on an Item, an Dialog should be shown.

    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View v, int position,long id)
        {
            System.out.println("LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
            String selectedFromList =(String) (lv.getItemAtPosition(position));
            final int kindid = db.selectIDvonKind(selectedFromList);

            try{

                AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
                adb.setTitle("Kind " + selectedFromList + " löschen?");
                adb
                .setMessage("Sind Sie sicher dass Sie " + selectedFromList + " löschen möchten?")
                .setCancelable(false)
                .setPositiveButton("Ja",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        db.deletekind(kindid);
                    }
                  })
                .setNegativeButton("Nein",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                    }
                });

            }
            catch(Exception e){
                Toast t = Toast.makeText(MainActivity.this, "Kind konnte nicht gelöscht werden!", Toast.LENGTH_SHORT);
                t.show();
            }
          return false;
        }
    });

My problem is, that I also have a OnItemClickListener:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() 
{
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {

        String selectedFromList =(String) (lv.getItemAtPosition(arg2));
        int id = db.selectIDvonKind(selectedFromList);
        System.out.println("   " + id);
        Intent i = new Intent(MainActivity.this, Ereignisse.class);
        i.putExtra("kinderid", id);
        startActivity(i);   
    }
}); 

So now, when I press long on an Item, the Dialog isn´t fired, just the action of onItemClick is executed.

Where´s my problem?

Was it helpful?

Solution

To show the AlertDialog you need to call the show() method on your dialog instance:

adb.show();

OTHER TIPS

You have'nt called show() method on the dialog.

also check that you've done following on your listview :-

    mListView.setLongClickable(true);
    mListView.setOnLongClickListener(this);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top