A problem with my AlertDialog

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

A problem with my AlertDialog

Postby E-Nature » Fri Jan 15, 2010 8:59 pm

hey all

I think I have a knot in my brain ;) I just can not figure out how I can get a value from a text-input within an AlertDialog. A method opens this alert after clicking on a menu item. my code looks like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         protected Dialog onCreateDialog(int id) {
  2.  
  3.                 switch (id) {
  4.  
  5.                 case 0:
  6.  
  7.                         (returns an alert.......)
  8.  
  9.                 case 1:
  10.  
  11.                         LayoutInflater factory = LayoutInflater.from(this);
  12.  
  13.                         final View textEntryView = factory.inflate(R.layout.register_member, null);
  14.  
  15.                         return new AlertDialog.Builder(CheatView.this).setIcon(R.drawable.alert_dialog_icon)
  16.  
  17. .setTitle(R.string.register_title)
  18.  
  19. .setView(textEntryView).setPositiveButton(R.string.ok,
  20.  
  21.                                         new DialogInterface.OnClickListener() {
  22.  
  23.                                                 public void onClick(DialogInterface dialog, int whichButton) {
  24.  
  25.  
  26.  
  27. //                                                      HERE I WANT TO GET THE VALUES OF THE INPUT
  28.  
  29.  
  30.  
  31.                                                 }
  32.  
  33.                                         }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
  34.  
  35.                                 public void onClick(DialogInterface dialog, int whichButton) {
  36.  
  37.  
  38.  
  39.                                         /* User clicked cancel so do some stuff */
  40.  
  41.                                 }
  42.  
  43.                         }).create();
  44.  
  45.                 }
  46.  
  47.                 return null;
  48.  
  49.         }
  50.  
  51.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


in case 1 I have an AlertDialog with some input fields. they are named username_edit, password_edit and email_edit in the layout XML.
how can I get the value which the user entered? For some reason I can't see the solution... :(

thanks for help!
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Top

Postby grzegorz.zajac » Fri Jan 15, 2010 9:40 pm

Hello,

As Dialog implements DialogInterface and the second one is a parameter for onClick() method try:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onClick(DialogInterface dialog, int whichButton) {
  2.  
  3.     EditText et = (EditText) dialog.findViewById(R.id.username_edit);
  4.  
  5.  
  6.  
  7.     String text = et.getText().toString();
  8.  
  9.    
  10.  
  11.     // do whatever else you need with et...
  12.  
  13. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Regards,
Grzesiek
grzegorz.zajac
Developer
Developer
 
Posts: 39
Joined: Wed Feb 18, 2009 8:37 pm
Location: Krakow/Poland

Postby E-Nature » Fri Jan 15, 2010 10:06 pm

thanks for the reply!
unfortunately this did not work. the problem is that I always get a NULL value which results in a forced close.
maybe I should mention that this method is in an Activity class and is not a class on its own.

I also tried this, but I also just get a null value:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. TextView tvPassword = (TextView) findViewById(R.id.password_view);
  2.  
  3. tvPassword.setText(et.getText());
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Postby E-Nature » Fri Jan 15, 2010 10:25 pm

ok me again, I tried now this without any success. I get the same null value and then the app crashes:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. .......
  3.  
  4. public void onClick(DialogInterface dialog, int whichButton) {
  5.  
  6.  
  7.  
  8.                                                         EditText editUsername = (EditText) findViewById(R.id.username_edit);
  9.  
  10.                                                         EditText editPassword = (EditText) findViewById(R.id.password_edit);
  11.  
  12.                                                         EditText editMail = (EditText) findViewById(R.id.email_edit);
  13.  
  14.                                                        
  15.  
  16.                                                         TextView tvUsername = (TextView) findViewById(R.id.username_view);
  17.  
  18.                                                         TextView tvPassword = (TextView) findViewById(R.id.password_view);
  19.  
  20.                                                         TextView tvMail = (TextView) findViewById(R.id.email_view);
  21.  
  22.  
  23.  
  24.                                                        
  25.  
  26.                                                        
  27.  
  28.                                                         String xxx = (String) tvUsername.getText();
  29.  
  30.                                                         String yyy = (String) tvPassword.getText();
  31.  
  32.                                                         String zzz = (String) tvMail.getText();
  33.  
  34. }
  35.  
  36. .......
  37.  
  38.                                                        
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


literally everything is NULL even though I entered a value into the EditText fields. Any ideas on that problem?
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Postby grzegorz.zajac » Fri Jan 15, 2010 11:28 pm

Hi,

Did you try to call findViewById() method on dialog, like I wrote in my first replay?
It seems you are still calling the findViewById() on the main activity object.
This probably should be called on your particular Dialog object.

EditText et = (EditText) dialog.findViewById(R.id.username_edit);

Is this also not working?

Regards,
Grzesiek
grzegorz.zajac
Developer
Developer
 
Posts: 39
Joined: Wed Feb 18, 2009 8:37 pm
Location: Krakow/Poland

Postby E-Nature » Fri Jan 15, 2010 11:34 pm

Yes, actually I tried it the way you suggested first

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. EditText et = (EditText) dialog.findViewById(R.id.username_edit);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


but Eclipse suggested me this correction:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. EditText et = (EditText) ((EditText) dialog).findViewById(R.id.username_edit);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


but the effect is the same. I get a null value and after clicking the OK button the application does a force close.
I can't seem to follow the reason why it crashes or why I get a null value :?
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Top

Postby grzegorz.zajac » Fri Jan 15, 2010 11:46 pm

Hi,

It crashes because the views from your dialog were not created during calls to findViewById().
And in the next lines you are trying to getText() on the null views.

Regards,
Grzesiek
grzegorz.zajac
Developer
Developer
 
Posts: 39
Joined: Wed Feb 18, 2009 8:37 pm
Location: Krakow/Poland

Postby E-Nature » Sat Jan 16, 2010 2:44 pm

Where would I have to put them exactly? I do not see many possibilities and where I tried I still had the same problem.

That's the class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class CheatView extends Activity implements OnClickListener {
  2.  
  3.  
  4.  
  5. (some variables)
  6.  
  7. private static final int MAX_PROGRESS = 100;
  8.  
  9. (...)
  10.  
  11.  
  12.  
  13. public void onCreate(Bundle savedInstanceState) {
  14.  
  15. (some code)
  16.  
  17. }
  18.  
  19.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


and there the method within this class which opens the dialog:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         protected Dialog onCreateDialog(int id) {
  2.  
  3.                 switch (id) {
  4.  
  5.                 case 0:
  6.  
  7.                         return new AlertDialog.Builder(CheatView.this).setTitle(R.string.report_cheat_title).setItems(R.array.report_reasons, new DialogInterface.OnClickListener() {
  8.  
  9.                                 public void onClick(DialogInterface dialog, int which) {
  10.  
  11. (some stuff which is not important right now...)
  12.  
  13.                                 }
  14.  
  15.                         }).create();
  16.  
  17.                 case 1:
  18.  
  19.                         LayoutInflater factory = LayoutInflater.from(this);
  20.  
  21.                         final View textEntryView = factory.inflate(R.layout.register_member, null);
  22.  
  23.                         return new AlertDialog.Builder(CheatView.this).setIcon(R.drawable.alert_dialog_icon).setTitle(R.string.register_title)
  24.  
  25. .setView(textEntryView).setPositiveButton(R.string.ok,
  26.  
  27.                                         new DialogInterface.OnClickListener() {
  28.  
  29.                                                 public void onClick(DialogInterface dialog, int whichButton) {
  30.  
  31.  
  32.  
  33.                                                         EditText editUsername = (EditText) findViewById(R.id.username_edit);
  34.  
  35.                                                         EditText editPassword = (EditText) findViewById(R.id.password_edit);
  36.  
  37.                                                         EditText editMail = (EditText) findViewById(R.id.email_edit);
  38.  
  39.                                                        
  40.  
  41.                                                         TextView tvUsername = (TextView) findViewById(R.id.username_view);
  42.  
  43.                                                         TextView tvPassword = (TextView) findViewById(R.id.password_view);
  44.  
  45.                                                         TextView tvMail = (TextView) findViewById(R.id.email_view);
  46.  
  47.                                                        
  48.  
  49.                                                         String xxx = (String) tvUsername.getText();
  50.  
  51.                                                         String yyy = (String) tvPassword.getText();
  52.  
  53.                                                         String zzz = (String) tvMail.getText();
  54.  
  55.                                                 }
  56.  
  57.                                         }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
  58.  
  59.                                 public void onClick(DialogInterface dialog, int whichButton) {
  60.  
  61.  
  62.  
  63.                                         /* User clicked cancel so do some stuff */
  64.  
  65.                                 }
  66.  
  67.                         }).create();
  68.  
  69.                 }
  70.  
  71.                 return null;
  72.  
  73.  
  74.  
  75.         }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


as you see the XML will be loaded in the line:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final View textEntryView = factory.inflate(R.layout.register_member, null);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

and this code will just be executed after the user clicked a menu-item to open this dialog.
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Postby jwei512 » Sat Jan 16, 2010 7:40 pm

I think it needs to be

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. LayoutInflater factory = LayoutInflater.from(this);
  3.  
  4.                final View textEntryView = factory.inflate(R.layout.register_member, null);
  5.  
  6.                return new AlertDialog.Builder(CheatView.this).setIcon(R.drawable.alert_dialog_icon).setTitle(R.string.register_title)
  7.  
  8. .setView(textEntryView).setPositiveButton(R.string.ok,
  9.  
  10.                          new DialogInterface.OnClickListener() {
  11.  
  12.                               public void onClick(DialogInterface dialog, int whichButton) {
  13.  
  14.  
  15.  
  16.                                    EditText editUsername = (EditText) textEntryView.findViewById(R.id.username_edit);
  17.  
  18.                                    EditText editPassword = (EditText) textEntryView.findViewById(R.id.password_edit);
  19.  
  20.                                    EditText editMail = (EditText) textEntryView .findViewById(R.id.email_edit);
  21.  
  22.                                    
  23.  
  24.                                    TextView tvUsername = (TextView) textEntryView .findViewById(R.id.username_view);
  25.  
  26.                                    TextView tvPassword = (TextView) textEntryView .findViewById(R.id.password_view);
  27.  
  28.                                    TextView tvMail = (TextView) textEntryView .findViewById(R.id.email_view);
  29.  
  30.                                    
  31.  
  32.                                    String xxx = (String) tvUsername.getText();
  33.  
  34.                                    String yyy = (String) tvPassword.getText();
  35.  
  36.                                    String zzz = (String) tvMail.getText();
  37.  
  38.                               }
  39.  
  40.                          }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
  41.  
  42.                     public void onClick(DialogInterface dialog, int whichButton) {
  43.  
  44.  
  45.  
  46.                          /* User clicked cancel so do some stuff */
  47.  
  48.                     }
  49.  
  50.                }).create();
  51.  
  52.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby E-Nature » Sun Jan 17, 2010 11:16 am

Perfect, that was it! Thanks, jwei512!

It works now 8)
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests