我试图在这里得到答案,但没有得到任何完美的答案。我正在尝试显示上下文菜单onItemLongClick
但没有成功,因为我同时使用onItemClick
和onItemLongClick
我正在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;
}