[英]How to show ContextMenu long click on listview android?
這是ListView
中的聯系人列表,我希望當用戶longClick
任何聯系人時, ContextMenu
彈出窗口應顯示為“ call”和“ send sms”。我為ContextMenu
編寫代碼,但ContextMenu
仍未顯示在longClick
請告訴我缺少的內容我的代碼。 這是MainaAtivity
類
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;
}
}
從您的代碼中刪除longpress方法,希望它可以工作
嘗試從以下位置交換行:
@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()
來處理用戶操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.