Android Gridview按钮适配器上下文菜单

Android Gridview按钮适配器上下文菜单,android,gridview,button,adapter,Android,Gridview,Button,Adapter,我有一本短语手册,能够将示例保存到SD。我使用Gridview设置,按钮适配器的代码如下: public View getView(int position, View convertView, ViewGroup parent) { try { final Sample sample = board.getSamples().get(position); if (sample != null) { Button button

我有一本短语手册,能够将示例保存到SD。我使用Gridview设置,按钮适配器的代码如下:

public View getView(int position, View convertView, ViewGroup parent) {
    try {
        final Sample sample = board.getSamples().get(position);

        if (sample != null) {

            Button button = new Button(context);
            button.setText(sample.getName());
            button.setTextColor(Color.WHITE); 
            button.setTextSize(12);
            button.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    context.play(sample);
                }

            });

            // TODO Implement this correctly.
             button.setOnLongClickListener(new OnLongClickListener() {
             public boolean onLongClick(View v) {

             return context.saveToSD(sample);


             }
             });

            return button;
        }
    } catch (IndexOutOfBoundsException e) {
        Log.e(getClass().getCanonicalName(), "No sample at position "
                + position);
    }

    return null;
}
我希望在这里集成一个长按上下文菜单,以提供保存样本的选项。我似乎无法在此方法中注册上下文菜单的按钮(即registerForContextMenu(按钮)),因为它会给我带来错误

我在这里有点不知所措,任何指点都会有很大帮助


谢谢

我认为这是一篇老文章,但今天我在寻找同一主题的答案时遇到了它。就像这里的问题一样,我有一个项目网格,希望在长时间单击时显示上下文菜单

我没有使用contextmenu,而是使用AlertDialog

gridview.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id)
        {
            showOptionsMenu(position);
            return true;
        }

    });

public void showOptionsMenu(int position)
{
    new AlertDialog.Builder(this)
    .setTitle("test").setCancelable(true).setItems(R.array.myOptions,
              new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialoginterface, int i) {
                       //take actions here according to what the user has selected
                   }
             }
    )
.show();
}
gridview.setOnItemLongClickListener(新的OnItemLongClickListener(){
公共布尔值长单击(AdapterView父对象、视图v、整型位置、长id)
{
显示选项菜单(位置);
返回true;
}
});
公共无效显示选项菜单(内部位置)
{
新建AlertDialog.Builder(此)
.setTitle(“测试”).setCancelable(true).setItems(R.array.myOptions,
新建DialogInterface.OnClickListener(){
公共void onClick(DialogInterface,inti){
//根据用户选择的内容在此处执行操作
}
}
)
.show();
}
希望这有帮助