如何在Android中的ListView中禁用(暗淡)onItemLongClick中的选项?

时间:2010-09-27 09:38:36

标签: android listview listviewitem

单击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

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

1 个答案:

答案 0 :(得分:0)

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

我假设您考虑过更简单的方法,就是不显示这些选项?也就是说,在特定时刻创建只包含有效字符串的数组副本?