How do I use onItemLongClick to display the context menu?
I tried to get an answer here but didn't get any perfect answer. I try to show the context menu onItemLongClick
, but do not have time because I use both onItemClick
, andonItemLongClick
I use onItemClick
to start a new activity but have 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;
}
source to share
I am trying to show context menu onItemLongClick
To use the context menu system, you don't implement OnItemLongClickListener
. Instead, you are calling registerForContextMenu()
(for example from an onCreate()
activity). Just remove OnItemLongClickListener
from your code shown above and you better be lucky.
source to share
Try the following:
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);
}
}
source to share