如何在列表视图 android 上长按显示上下文菜单?

How to show ContextMenu long click on listview android?

这里是 ListView 中的联系人列表,我希望当用户 longClick 使用任何联系人时 ContextMenu 弹出窗口应该显示 "call" 和 "send sms" 我为 ContextMenu 编写了代码,但 ContextMenu 仍然没有显示在 longClick 上,请告诉我我的代码中缺少什么。 这里是 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;


    }


}

尝试换行:

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

更新

删除 listView.setOnItemLongClickListener 因为您不能同时使用 longclicklistener 和上下文菜单。要显示上下文菜单,您只需在扩充视图后调用 registerForContextMenu(listView) 并覆盖 onCreateContextMenu() 以创建菜单,并覆盖 onContextItemSelected() 以处理用户操作。

只需从您的代码中删除 longpress 方法,希望它能正常工作