此处控件未输入ContextMenu
而未打印吐司。我不知道为什么?我在这里缺少什么,只是我想长按显示上下文菜单,我尝试了很多天但仍然无法解决它。
我从昨天尝试了许多方法,但仍然没有工作,请帮助我,我在这里失踪了。
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;
}
});
}
//Here control is not entering in Contextmenu why?
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
Toast.makeText(MainActivity.this, "Entering Context Menu", Toast.LENGTH_SHORT).show();
getMenuInflater().inflate(R.menu.main, menu);
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();
return true;
}
}