Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use onItemLongClick to show a context menu?

I tried to get answer here but didn't get any perfect answer. I am trying to show context menu on onItemLongClick but no success as i am using both onItemClick and onItemLongClick

i am using onItemClick to start a new activity but no success on both.

Here is the code

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_contacts);  

    contactList = new ArrayList<HashMap<String,String>>();

    new LoadAllContacts().execute();

    registerForContextMenu(getListView());

    ListView listView = getListView();

    listView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int postion, long id) {
                registerForContextMenu( view );
                openContextMenu( view ); 
            return true;
        }
    });
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int postion, long id) {
            Intent intent = new Intent(AllContactsActivity.this, editContactActivity.class);    
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == 100)
    {
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.listview_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.editContactMI:
        Intent i = new  Intent(getApplicationContext(), editContactActivity.class);
        i.putExtra(TAG_ID, cId);
        i.putExtra(TAG_NAME, cName);
        i.putExtra(TAG_CONTACT_NO, cNumber);
        startActivityForResult(i, 100);
        cId = null;
        cName = null;
        cNumber = null;
        break;
    case R.id.deleteContactMI :
        new DeleteContact().execute();
        break;
    case R.id.saveContactMI:
        break;
    default:
        cId = null;
        cName = null;
        cNumber = null;
        break;
    }

    return true;
}
like image 946
Akmal Rasool Avatar asked Jan 15 '23 04:01

Akmal Rasool


1 Answers

I am trying to show context menu on onItemLongClick

To use the context menu system, you do not implement an OnItemLongClickListener. Instead, you call registerForContextMenu() (e.g., from onCreate() of the activity). Simply delete your OnItemLongClickListener from your code shown above, and you should have better luck.

like image 162
CommonsWare Avatar answered Jan 19 '23 20:01

CommonsWare