0

I am trying to make a dialog that basically asks for the genre of the song from the user. Thus I have two buttons and an EditText. I want to fetch the string from EditText when I hit the "Save" button and save it to the database. but the problem is the EditText is returning and empty string which can be due to the fact that I just can't relate the Edittext to the one in the layout. Code Snippet:

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

    }

Can anyone tell me the problem please?

4

1 に答える 1

0

in the view= line, you create another instance than the one used in the dialog.

Do this instead

view = inflator.inflate(R.layout.ask_genre, null);
builder.setView(view);
于 2012-10-27T19:19:15.710 に答える