4

我试图在这里得到答案,但没有得到任何完美的答案。我正在尝试显示上下文菜单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;
}
4

2 回答 2

6

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

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

于 2013-02-17T18:24:37.810 回答
3

试试这个:

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);
    }
}
于 2015-03-19T06:07:29.677 回答