-8

This is the top half of the class that hosts the dialog.

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MovieAdd extends Activity {

private EditText mTitleText;
private Button mSavebutton;
private Long mRowId;
private MyMoviesDBAdapter mDbHelper;
private String genrename;
private TextView mGenretext;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setSoftInputMode     (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

    mDbHelper = new MyMoviesDBAdapter(this);

    setContentView(R.layout.add_movie_dialog);


    mTitleText = (EditText) findViewById(R.id.mMovietitle);
    mGenretext = (TextView) findViewById(R.id.genre_text);


    mSavebutton = (Button) findViewById(R.id.mSavebutton);

    mRowId = savedInstanceState != null ? savedInstanceState.getLong(MyMoviesDBAdapter.KEY_ROWID) 
        : null;

    registerButtonListeners();
    onCreateDialog(savedInstanceState);

}

the problem is that when I use this code, the TextView is always set to "Thriller" the "which" variable seems to always be initializing to 14. I'm not even sure this is the best method to achieve what I am trying to do as later, after the dialog is closed, the "genrename" variable is then passed into a database.

 public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(MovieAdd.this);
    builder.setTitle(R.string.select_genre);
           builder.setItems(R.array.genres, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
               // The 'which' argument contains the index position
               // of the selected item

            switch(which) {
                case 0:
                    genrename = "Action";
                case 1:
                    genrename = "Anime";
                case 2:
                    genrename = "Childrens";
                case 3:
                    genrename = "Classics";
                case 4:
                    genrename = "Comedy";
                case 5:
                    genrename = "Cult";
                case 6:
                    genrename = "Documentary";
                case 7:
                    genrename = "Drama";
                case 8:
                    genrename = "Foreign";
                case 9:
                    genrename = "Horror";
                case 10:
                    genrename = "Music";
                case 11:
                    genrename = "Romance";
                case 12:
                    genrename = "Sci-fi";
                case 13:
                    genrename = "Sports";
                case 14:
                    genrename = "Thriller";                 
                }
            mGenretext.setText(genrename);

           }
    });


    return builder.show();
}
4

3 に答える 3

0

you need to use break in each case.

public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(MovieAdd.this);
builder.setTitle(R.string.select_genre);
       builder.setItems(R.array.genres, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
           // The 'which' argument contains the index position
           // of the selected item

        switch(which) {
            case 0:
                genrename = "Action";
             break;                
            case 1:
                genrename = "Anime";
             break;                
            case 2:
                genrename = "Childrens";
             break;                
            case 3:
                genrename = "Classics";
             break;                
            case 4:
                genrename = "Comedy";
             break;                
            case 5:
                genrename = "Cult";
            break;                
            case 6:
                genrename = "Documentary";
            break;                
            case 7:
                genrename = "Drama";
            break;                
            case 8:
                genrename = "Foreign";
            break;                
            case 9:
                genrename = "Horror";
            break;                
            case 10:
                genrename = "Music";
            break;                
            case 11:
                genrename = "Romance";
            break;                
            case 12:
                genrename = "Sci-fi";
            break;                
            case 13:
                genrename = "Sports";
            break;                
            case 14:
                genrename = "Thriller"; 
             break;                

            }
        mGenretext.setText(genrename);

       }
});


return builder.show();
}
于 2013-01-24T07:11:03.603 に答える
0

Add the break; in your each cases as below:

 public void onClick(DialogInterface dialog, int which) {
           // The 'which' argument contains the index position
           // of the selected item
        switch(which) {
            case 0:
                genrename = "Action";
              break;
            case 1:
                genrename = "Anime";
              break;
            case 2:
                genrename = "Childrens";
            case 3:
                genrename = "Classics";
            case 4:
                genrename = "Comedy";
               break;
                ....................................             
            }
        mGenretext.setText(genrename);
       }
});
于 2013-01-24T07:11:21.827 に答える
0

Use break in every case of switch else the control will fall down to last case.For that reason only it is showing only Thriller.

于 2013-01-24T07:12:07.567 に答える