-8

这是承载对话的类的上半部分。

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

}

问题是,当我使用此代码时,TextView 总是设置为“Thriller”,“which”变量似乎总是初始化为 14。我什至不确定这是实现我想要的最佳方法稍后再做,在对话框关闭后,“genrename”变量然后被传递到数据库中。

 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

您需要break在每种情况下使用。

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

在您的每种情况下添加break;如下:

 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

在开关的每一种情况下使用break,否则控制将下降到最后一种情况。因此,它只显示Thriller

于 2013-01-24T07:12:07.567 回答