我正在尝试制作一个对话框,基本上要求用户提供歌曲的类型。 因此我有两个按钮和一个EditText。当我点击“保存”按钮并将其保存到数据库时,我想从EditText中获取字符串。但问题是EditText返回并且是空字符串,这可能是因为我无法将Edittext与布局中的Edittext相关联。代码段:
class AskGenreDialog {
private String songTitle = "";
private int id;
EditText AskGenre;
View view;
public void setSongTitle(String songTitle){
this.songTitle = songTitle;
}
public void setId(int id){
this.id = id;
}
public void onCreateDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(MusicPlayerActivity.this);
LayoutInflater inflator = (LayoutInflater)MusicPlayerActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder.setView(inflator.inflate(R.layout.ask_genre, null));
builder.setTitle("Genre Not Found").setMessage(songTitle);
view = inflator.inflate(R.layout.ask_genre, null);
AskGenre = (EditText) view.findViewById(R.id.genre);
builder.setPositiveButton("Save", new OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
String genre =AskGenre.getText().toString();
handler.setGenreForId(genre,id);
Log.d("Ans:", genre);
}
});
builder.setNegativeButton("Don't Know", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
// create alert dialog
AlertDialog alertDialog = builder.create();
// show it
alertDialog.show();
}
}
有人可以告诉我这个问题吗?
答案 0 :(得分:0)
在view =行中,您创建的另一个实例不是对话框中使用的实例。
改为
view = inflator.inflate(R.layout.ask_genre, null);
builder.setView(view);