Text Entry Dialog return null,not the same as the post below

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

Text Entry Dialog return null,not the same as the post below

Postby Helios » Tue Jan 20, 2009 10:48 pm

Hi every one, I have the same problem explained in the post below, but I implemented my code in a different way and I have tried the different solutions that I have find but no one has fixed the problem. I will show my .xml and .java code for any one that can help me.

XML code (pedir_tel.xml)
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <EditText xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
  4. android:id="@+id/tel_envio"
  5.  
  6. android:textSize="12px"
  7.  
  8. android:textColor="#000"
  9.  
  10. android:layout_width="wrap_content"
  11.  
  12. android:layout_height="wrap_content"
  13.  
  14. android:gravity="center_vertical"
  15.  
  16. android:padding="5sp"
  17.  
  18. />
  19.  
  20.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


JAVA code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class Tel_Request extends Activity
  3.  
  4. {
  5.  
  6.         private static final int DIALOG_TEXT_ENTRY = 7;
  7.  
  8.         private EditText numero_tel;
  9.  
  10.        
  11.  
  12.         @Override
  13.  
  14.     protected Dialog onCreateDialog(int id)
  15.  
  16.         {
  17.  
  18.                 if (id == DIALOG_TEXT_ENTRY)
  19.  
  20.                 {
  21.  
  22.                         LayoutInflater factory = LayoutInflater.from(this);
  23.  
  24.                         final View textEntryView = factory.inflate(R.layout.pedir_tel, null);
  25.  
  26.        
  27.  
  28.                         return new AlertDialog.Builder(Tel_Request.this)
  29.  
  30.                         .setTitle("Ingresar número telefónico")
  31.  
  32.                         .setView(textEntryView)
  33.  
  34.        
  35.  
  36.                         .setPositiveButton("Enviar", new DialogInterface.OnClickListener()
  37.  
  38.                         {
  39.  
  40.                                 public void onClick(DialogInterface dialog, int whichButton)
  41.  
  42.                                 {
  43.  
  44.                                         numero_tel = (EditText) textEntryView.findViewById(R.id.tel_envio);
  45.  
  46.                                         String tele = numero_tel.toString();
  47.  
  48.                                        
  49.  
  50.                                         if ( tele != null)
  51.  
  52.                                                 mostrar(tele);
  53.  
  54.                                         else
  55.  
  56.                                                 mostrar ("hello world");
  57.  
  58.                                 }
  59.  
  60.                         })
  61.  
  62.                        
  63.  
  64.                         .setNegativeButton("Cancelar", new DialogInterface.OnClickListener()
  65.  
  66.                         {
  67.  
  68.                                 public void onClick(DialogInterface dialog, int whichButton)
  69.  
  70.                                 {
  71.  
  72.                                         setResult(RESULT_OK);
  73.  
  74.                         finish();
  75.  
  76.                                 }
  77.  
  78.                         })
  79.  
  80.                         .create();
  81.  
  82.                 }
  83.  
  84.                 return null;
  85.  
  86.         }
  87.  
  88.        
  89.  
  90.     @Override
  91.  
  92.     protected void onCreate(Bundle savedInstanceState)
  93.  
  94.     {
  95.  
  96.         super.onCreate(savedInstanceState);
  97.  
  98.         showDialog(DIALOG_TEXT_ENTRY);
  99.  
  100.     }
  101.  
  102.    
  103.  
  104.     private void mostrar(String s)
  105.  
  106.     {
  107.  
  108.         Toast toast = Toast.makeText(this, s, Toast.LENGTH_LONG);
  109.  
  110.         toast.show();
  111.  
  112.     }
  113.  
  114. }
  115.  
  116.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


The curious fact is that in the toast message doesn't show the "hello world" message, it shows "Android.widget.EditText@4343fa80" (the number varies). I will be very thankful to the person that could help me becausa I been all day long trying to fix this.

Regards,
Helios
Helios
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Jan 17, 2009 7:11 am

Top

Postby MrSnowflake » Tue Jan 20, 2009 11:11 pm

That's because:
String tele = numero_tel.toString();
is the toString() of the EditText itself, not the data contained in the EditText. Android is implemented as MVC, so to ge the text in the EditText you should do:
numero_tel.getText().toString()

And the number varies, because the number is (I believe) the hash used by Java, which is different with every instantiation.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

PROBLEM SOLVED

Postby Helios » Wed Jan 21, 2009 4:09 am

Thanks a lot! Your answer solved my problem, I didn't notice that detail before. Thanks again.

Regards,
Helios
Helios
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Jan 17, 2009 7:11 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests