Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Capturing Photos and Videos Presenting a List of Choices in a Dialog

Ricky Sparks
Ricky Sparks
22,249 Points

Code Challenge Help??? What's wrong with my code?

We have a button in the ActionBar that will present a list of choices to the user. In the switch statement below, start by declaring an AlertDialog Builder and initializing it with the constructor. Use this for the context.

MainActivity.java
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.MenuItem;

public class MainActivity extends Activity {

    protected DialogInterface.OnClickListener mDialogListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    };

    // Some code omitted!

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();

        switch(itemId) {        
            case R.id.action_button:
                break;
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setItems(R.array.list_choices, mDialogListener);
        }

        return super.onOptionsItemSelected(item);

    }
}
Paolo Scamardella
Paolo Scamardella
24,828 Points

I'm not sure what you are trying to do, but after doing a quick view of your code, you switch statement is wrong. Your break statement is before your code, which means it will never get executed. Your code needs come first.

switch(itemId) {        
            case R.id.action_button:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setItems(R.array.list_choices, mDialogListener);
                break;
}

1 Answer

Sorry, Paolo,

but this may get Ricky further if he is dealing with the

"Presenting a List of Choices in Dialog Challenge": http://teamtreehouse.com/library/presenting-a-list-of-choices-in-a-dialog

switch(itemId) {        
            case R.id.action_button:
                 //for challenge 1 of 3
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                 //for challenge 2 of 3
                builder.setItems(R.array.genres, mDialogListener);
                //for challenge 3 of 3
                AlertDialog dialog = builder.create();
                dialog.show();

                break;
}