请在下面找到代码示例: public class Abc extends Activity implements OnClickListener{ private ListView displayList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.l
public class Abc extends Activity implements OnClickListener{ private ListView displayList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainlayout); displayList =(ListView)findViewById(R.id.addressbooklistview); addressbookAdapter = new CustomListAdapter(this,addressbookList); displayList.setAdapter(addressbookAdapter); registerForContextMenu(displayList); } @Override public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { Log.e("", "Entered Context Menu"); } public void onClick(View v) { Log.e("", "Click Detected"); } }
我无法长按调用上下文菜单.
请让我知道任何解决方案.
我的问题是我实际上在Listview项目中的TextView上有onClick()代码,而不是ListView本身.据推测,它正在窃取列表视图中的点击.为了解决这个问题,我删除了那段代码,在我的Activity的onCreate方法中,我为ListView调用了setOnItemClickListener().所以现在我有这个:
// This creates the context menu functionality. registerForContextMenu(findViewById(R.id.list_item)); // This creates the click functionality for the listview item. ListView listView = (ListView) findViewById(R.id.list_item); listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { // code here } });