here is a list of contact in ListView , i want when user longClick on any contacts then ContextMenu popup should be show "call"and "send sms" i write code for ContextMenu but still ContextMenu not showing on longClick please tell me what is missing in my code.
here is MainaAtivity class
public class MainActivity extends AppCompatActivity {
ListView listView;
Button sync;
ProgressDialog progressDialog;
String name, phone;
ArrayList<Contact_list> listitem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listitem = new ArrayList<Contact_list>();
listView = (ListView) findViewById(R.id.listViewID);
registerForContextMenu(listView);
sync= (Button) findViewById(R.id.syncID);
sync.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// GET CONTACTS DATA
GetContactsIntoArrayList();
listView.setAdapter(new Custom_adapter(MainActivity.this, listitem));
Toast.makeText(MainActivity.this, "import", Toast.LENGTH_SHORT).show();
}
});
}
public void GetContactsIntoArrayList(){
Cursor cursor;
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);
while (cursor.moveToNext()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
listitem.add(new Contact_list(name,phone));
listView.setAdapter(new Custom_adapter(MainActivity.this, listitem));
}
cursor.close();
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int index, long arg3) {
// TODO Auto-generated method stub
Log.v("long clicked","pos: " + index);
//Log.d("tag", "message");
String str=listView.getItemAtPosition(index).toString();
Log.d("long click sucessfull: " ,str);
return true;
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Call");
menu.add(0, v.getId(), 0, "Send SMS");
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
// info.position will give the index of selected item
int IndexSelected=info.position;
if(item.getTitle()=="Call")
{
// Code to execute when clicked on This Item
}
else if(item.getTitle()=="Send SMS")
{
// Code to execute when clicked on This Item
//
}
else
{
return false;
}
return true;
}
}
Remove longpress method from your code just, hope it will working
Try to swap the lines from:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Call");
menu.add(0, v.getId(), 0, "Send SMS");
}
To:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Call");
menu.add(0, v.getId(), 0, "Send SMS");
super.onCreateContextMenu(menu, v, menuInfo);
}
Update
Remove listView.setOnItemLongClickListener because you can't use a longclicklistener and a context menu at the same time. To show a context menu you just need to call registerForContextMenu(listView) after you inflated your view and override onCreateContextMenu() for menu creation and onContextItemSelected() to handle the user action.
来源:https://stackoverflow.com/questions/41982635/how-to-show-contextmenu-long-click-on-listview-android