-1

I am trying to implement onItemLongClickListener and onItemClickListener event on listview row but the problem is that when I longPress the listview row and release it then both events get called at the same time. Please give me solution how can I achive this.

here is my code

listregional.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        final CharSequence[] dialogregion = {"Edit","Delete"};
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Pilih Menu");
        builder.setItems(dialogregion, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int region) {
                switch (region){
                    case 0:
                        Toast.makeText(getActivity(),"Edited",Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(getActivity(),"Deleted",Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            });

        return false;
    }
});

please help me

Dave
  • 1,784
  • 2
  • 23
  • 35
ErrorMan
  • 13
  • 1

4 Answers4

1
 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// your code
return true
}

return true instead of return false will prevent click event to be continue.

BOUTERBIAT Oualid
  • 1,494
  • 13
  • 15
0

You have to signal other listeners that the dispatched event is consumed by returning true

chris
  • 106
  • 3
0

Return boolean true:

 listregional.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            final CharSequence[] dialogregion = {"Edit","Delete"};
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Pilih Menu");
            builder.setItems(dialogregion, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int region) {
                   switch (region){
                       case 0:
                           Toast.makeText(getActivity(),"Edited",Toast.LENGTH_SHORT).show();
                           break;
                       case 1:
                           Toast.makeText(getActivity(),"Deleted",Toast.LENGTH_SHORT).show();
                           break;
                   }
                }
            });
            return true;
        }
    });
Vivek_Neel
  • 1,343
  • 1
  • 14
  • 25
Franklyn
  • 325
  • 2
  • 8
0

Handling long press events is essentially the same as handling clicks except that you return a boolean which specifies whether Android should continue to propagate the click event.

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id){
    return onLongListItemClick(v,pos,id);
}

});

If your long-click method returns true then you are telling Android that you handled the event. If your method returns false, Android will still call other handlers such as your onItemClick handler.

protected boolean onLongListItemClick(View v, int pos, long id) {
Log.i(TAG, "onLongListItemClick id=" + id);
return true;

}

so change the last line of your code to return TRUE.

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62