我试图理解用于创建上下文菜单的 android API 指南,但它们确实没有对应该如何使用 AdapterContextMenuInfo 给出任何解释。
我的代码:
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.internal.widget.IcsAdapterView.AdapterContextMenuInfo;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
...
public class MyActivity extends SherlockActivity {}
...
class MyCursorAdapter extends CursorAdapter {
...
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_row, parent, false);
registerForContextMenu(rowView);
return rowView;
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
android.view.MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context, menu);
}
@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
Log.d(MyApp.LOG_TAG, "info is null: "+ (info == null)); // info is null: true
...
}
}
根据我对上下文菜单应该如何工作的模糊理解,我希望 info 包含 db row id 或至少包含我可以从中检索 id 作为标签的 targetView 。
请让我知道我哪里出错了,或者我如何检测上下文菜单显示的 ListView 项。