如何使用onItemLongClick显示上下文菜单?

时间:2013-02-17 18:20:30

标签: android android-contextmenu

我试图在这里得到答案,但没有得到任何完美的答案。 我正在尝试在onItemLongClick上显示上下文菜单,但没有成功,因为我同时使用onItemClickonItemLongClick

我正在使用onItemClick开始一项新活动但两项都没有成功。

这是代码

    @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;
}

2 个答案:

答案 0 :(得分:6)

  

我正在尝试在onItemLongClick上显示上下文菜单

要使用上下文菜单系统,请不要实现OnItemLongClickListener。相反,您拨打registerForContextMenu()(例如,来自活动的onCreate())。只需从上面显示的代码中删除OnItemLongClickListener,您就应该有更好的运气。

答案 1 :(得分:2)

试试这个:

listView.setOnItemLongClickListener(this);

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    Cursor c_index = (Cursor) parent.getItemAtPosition(position);
    detail_id = c_index.getInt(c_index.getColumnIndexOrThrow(DbAdapter.KEY_RID));
    registerForContextMenu( parent );
    openContextMenu( parent );
    return true;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    if (v.getId()==R.id.list) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_context, menu);
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_details:
            //some code
            return true;
        case R.id.action_share:
            //some code
            return true;
        case R.id.action_del:
            //enter code here`
           return true;
        default:
            return super.onContextItemSelected(item);
    }
}