Gridview 按钮适配器上下文菜单

发布于 2024-11-04 02:03:50 字数 1230 浏览 0 评论 0原文

我有一本短语手册,能够将示例保存到 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(button),因为它给了我错误。

我在这里有点困惑,任何指针都会有很大的帮助。

谢谢

I have a phrasebook, with the ability to save the sample to SD. I use a Gridview set up with the following code in place for the button adapter:

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;
}

I am looking to integrate a context menu here on a Long press, to give the option of where to save the sample. I don't seem to be able to register the button for the context menu within this method (ie registerForContextMenu(button), as it gives me errors.

I am a bit stumped here, any pointers would be a great help.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乜一 2024-11-11 02:03:50

我认为这是一篇旧帖子,但我今天遇到它,因为我正在寻找同一主题的答案。就像这里的问题一样,我有一个项目网格,并且想在长按时显示上下文菜单。

我没有使用上下文菜单,而是使用 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();
}

希望这有帮助。

I take it that this is an old post but I came across it today as I was looking for an answer on the same topic. Like the question here I have a grid of items and wanted to show a context menu on long click.

I am not using a contextmenu but instead I am using an 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();
}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文