这是承载对话的类的上半部分。
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();
}