Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch between onItemClick and onItemLongClick event of Android ListView

I want to ask how to switch and select only one event under the listview. My code below is working. But when the OnItemLongClick fires, the OnItemClick also fires. How can I switch the event where only one event will be detected:

lstResult.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                // TODO Auto-generated method stub

                //showToast(arg0.getItemIdAtPosition(position) +  "");
                 String str = searchWhere(lstResult.getItemAtPosition(position) + "");
                 String word = lstResult.getItemAtPosition(position).toString();
                 showDialog(word,str);
            }

        });
        lstResult.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                // TODO Auto-generated method stub
                showToast(lstResult.getItemAtPosition(position) + "");
                return false;
            }

        });
like image 848
ajdeguzman Avatar asked Oct 23 '13 08:10

ajdeguzman


2 Answers

According to documentation of OnItemLongClickListener:

Returns true if the callback consumed the long click, false otherwise

You should return true if long click is fired.

like image 132
traninho Avatar answered Jan 01 '23 07:01

traninho


Return boolean true at the end of OnItemLongClick.

like image 40
Martin Avatar answered Jan 01 '23 07:01

Martin