在为ListView设置自定义适配器并向ContextMenu注册后,看起来根本没有调用onCreateContextMenu。我究竟做错了什么?这是我的主要活动代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbAdapter.open();
workersArray=dbAdapter.getAllWorkersAndHoursToday();
dbAdapter.close();
ListView listView = (ListView) findViewById(R.id.workers_list);
CustomArrayAdapter adapter = new CustomArrayAdapter(this, R.layout.workers_row, R.id.workers_name, workersArray);
this.adapter=adapter;
listView.setAdapter(adapter);
registerForContextMenu(listView);
}
@Override
protected void onStart() {
super.onStart();
if (Utils.isSaved(this)){
Intent intent = new Intent(this, SavedActivity.class);
startActivity(intent);
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
Log.d("debug","Called onCreateContextMenu");
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_popup_main, menu);
}
和自定义ArrayAdapter的getView:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WorkerHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WorkerHolder();
holder.txtName = (TextView) row.findViewById(R.id.workers_name);
holder.txtHours = (TextView) row.findViewById(R.id.worked_hours_today);
row.setTag(holder);
} else {
holder = (WorkerHolder) row.getTag();
}
Worker worker = data[position];
holder.txtName.setText(worker.getName());
holder.txtHours.setText("" + worker.getHoursWorkedToday());
return row;
}
感谢您的回答