views:

102

answers:

2

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).

A: 

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?

mikeplate
A: 

Yeah but it's a big pain because I have three different menus depending on what item was clicked. I've settled for now but I will have to rewrite everything because it's highly inefficient the way I'm doing it now. Astro file explorer does this but I'm pretty sure he uses a custom implementation. I'm going to rewrite it based on the example on your web site because the code is cleaner by I'll try and write something better sometime down the line.

Aaron Ratner