如何从 Android 中的 ListView 禁用(调暗)onItemLongClick 中的选项?

发布于 2024-09-24 17:57:19 字数 1730 浏览 2 评论 0原文

当单击 ListView 中的某个项目时,我会在对话框中弹出几个选项供用户选择。但是,在不同的情况下,我想禁用一个或多个选项,以便用户无法选择它们。这是一些代码。

public class MyApp extends ListActivity implements OnItemClickListener, OnItemLongClickListener {

private static final int HOW_MANY = 4;
private static final int ADD = 0;
private static final int EDIT = 1;
private static final int OPEN = 2;
private static final int DELETE = 3;

private String[] myItemClickDialog = null;
...

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.mylayout);


    this.myItemClickDialog = new String[HOW_MANY];
    this.myItemClickDialog[ADD] = "Add";
    this.myItemClickDialog[EDIT] = "Edit";
    this.myItemClickDialog[OPEN] = "Open";
    this.myItemClickDialog[DELETE] = "Delete";
}

@Override
public final boolean onItemLongClick(final AdapterView<?> parent,
    final View view, final int position, final long id) {

    final Context context = this;
    Builder builder = new Builder(context);
    builder.setTitle("Options");

    String[] items = this.myItemClickDialog;
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(final DialogInterface dialog, final int which) {
            switch (which) {
                case ADD:
                    //Do stuff
                    break;
                case EDIT:
                    //Do stuff
                    break;
                case OPEN:
                    //Do stuff
                    break;
                case DELETE:
                    //Do stuff
                    break;
            }
        }
// Rest of code here

我希望能够禁用(通过调暗)特定选项,例如删除选项或打开选项,具体取决于单击的列表项(我覆盖了该部分,即检测部分)。

When an item in my ListView is clicked, I have several options pop up in a dialog for the user to choose. However, there are varying scenarios where I'd like to disable one or more options so the user cannot choose them. Here's some code.

public class MyApp extends ListActivity implements OnItemClickListener, OnItemLongClickListener {

private static final int HOW_MANY = 4;
private static final int ADD = 0;
private static final int EDIT = 1;
private static final int OPEN = 2;
private static final int DELETE = 3;

private String[] myItemClickDialog = null;
...

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.mylayout);


    this.myItemClickDialog = new String[HOW_MANY];
    this.myItemClickDialog[ADD] = "Add";
    this.myItemClickDialog[EDIT] = "Edit";
    this.myItemClickDialog[OPEN] = "Open";
    this.myItemClickDialog[DELETE] = "Delete";
}

@Override
public final boolean onItemLongClick(final AdapterView<?> parent,
    final View view, final int position, final long id) {

    final Context context = this;
    Builder builder = new Builder(context);
    builder.setTitle("Options");

    String[] items = this.myItemClickDialog;
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(final DialogInterface dialog, final int which) {
            switch (which) {
                case ADD:
                    //Do stuff
                    break;
                case EDIT:
                    //Do stuff
                    break;
                case OPEN:
                    //Do stuff
                    break;
                case DELETE:
                    //Do stuff
                    break;
            }
        }
// Rest of code here

I would like to be able to disable (by dimming) a particular option, such as the DELETE option or the OPEN option depending on which list item was clicked (I got that part covered, that is, the detection piece).

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

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

发布评论

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

评论(1

想挽留 2024-10-01 17:57:19

不幸的是,这似乎不可能。 AlertDialog.Builder 系统在其自定义选项中没有考虑到这一点。我查看了 createListView 来验证这一点。

我想您已经考虑过不显示这些选项的更简单的方法?也就是说,仅使用该特定时刻的有效字符串创建数组的副本?

Unfortunately this does not seem possible. The AlertDialog.Builder system hasn't taken this into account in its customization options. I looked at the source code for createListView to verify this.

I assume you've considered the simpler approach of just not showing those options? That is, creating a copy of the array with only the valid strings at that particular moment?

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