EditText not saving data?

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

EditText not saving data?

Postby Draffodx » Thu Jan 07, 2010 2:02 pm

I have an application that is just a basic settings app where a user can check settings, the settings appear in an edittext and the user can then edit the settings and then click save.

here is my code

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. EditText nameEdit = (EditText)findViewById(R.id.VoipUnEdit);
  2.  
  3.                 nameEdit.setText(name);
  4.  
  5.                
  6.  
  7.                 EditText passEdit = (EditText)findViewById(R.id.VoipPasswordEdit);
  8.  
  9.                 passEdit.setText(password);
  10.  
  11.                
  12.  
  13.                 EditText authEdit = (EditText)findViewById(R.id.VoipAuthEdit);
  14.  
  15.                 authEdit.setText(auth);
  16.  
  17.                
  18.  
  19.                 EditText displayEdit = (EditText)findViewById(R.id.VoipDisplayNameEdit);
  20.  
  21.                 displayEdit.setText(displayName);
  22.  
  23.                
  24.  
  25.                 EditText proxyEdit = (EditText)findViewById(R.id.SipProxyEdit);
  26.  
  27.                 proxyEdit.setText(sipProxy);
  28.  
  29.                
  30.  
  31.                 EditText realmEdit = (EditText)findViewById(R.id.SipRealmEdit);
  32.  
  33.                 realmEdit.setText(sipRealm);
  34.  
  35.                
  36.  
  37.                 EditText registrarEdit = (EditText)findViewById(R.id.SipRegistrarEdit);
  38.  
  39.                 registrarEdit.setText(sipRegistrar);
  40.  
  41.                
  42.  
  43.                 EditText expiresTimeEdit = (EditText)findViewById(R.id.expiresTimeEdit);
  44.  
  45.                 expiresTimeEdit.setText(expiresTime.toString());
  46.  
  47.        
  48.  
  49.                 String time = expiresTimeEdit.getText().toString();
  50.  
  51.                 int timee = Integer.parseInt(time);
  52.  
  53.                
  54.  
  55.                 final Settings newSettings = new Settings(nameEdit.getText().toString(), authEdit.getText().toString(), passEdit.getText().toString()
  56.  
  57.                                 ,displayEdit.getText().toString(), proxyEdit.getText().toString(), registrarEdit.getText().toString(),
  58.  
  59.                                 realmEdit.getText().toString(),timee );
  60.  
  61.                
  62.  
  63.                 final Button submit_Button = (Button) findViewById(R.id.submit);
  64.  
  65.                 final String nameE = nameEdit.getText().toString();
  66.  
  67.                 submit_Button.setOnClickListener(new View.OnClickListener(){
  68.  
  69.                         public void onClick(View v){
  70.  
  71.                                 Log.d("SETTINGS DATA", "NameEdit= " + nameE);
  72.  
  73.                                 newSettings.set_Id("0");
  74.  
  75.                                 updateSettings(2, newSettings);
  76.  
  77.                         }
  78.  
  79.                        
  80.  
  81.                 });
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


So the data is pulled into the EditText's from an array and it displays correctly, however when I change the text in an EditText and then click on the button it pulls the data that was in the EditText before it was changed.

Can anyone explain why this would happen? Is there something I'm missing?
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Top

Postby jwei512 » Thu Jan 07, 2010 10:42 pm

Uhh so I'm not entirely sure what you're code is trying to do...

For example... if you take a look at:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. EditText nameEdit = (EditText)findViewById(R.id.VoipUnEdit);
  3.  
  4. nameEdit.setText(name);
  5.  
  6.  
  7.  
  8. // ...
  9.  
  10.  
  11.  
  12. final String nameE = nameEdit.getText().toString();
  13.  
  14. submit_Button.setOnClickListener(new View.OnClickListener(){
  15.  
  16.      public void onClick(View v){
  17.  
  18.           Log.d("SETTINGS DATA", "NameEdit= " + nameE);
  19.  
  20.           newSettings.set_Id("0");
  21.  
  22.           updateSettings(2, newSettings);
  23.  
  24.       }
  25.  
  26. });
  27.  
  28.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


So basically it seems like you are retrieving your EditText called nameEdit, which is fine. Then you set the text to be some String "name". Then you get back that same string with

final String nameE = nameEdit.getText().toString();

And so here, you will have nameE = name. Then nameE is declared final... so it will never change, and everytime you click your button you will just print it out.

I'm not sure what this "Settings" object is or what newSettings.set_Id does but if you're expecting the Log.d to print out something different each time you click your button - it's not.

Is this what's supposed to happen? If it's not, then retrieving the edit text content is easy.
- 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 Draffodx » Fri Jan 08, 2010 10:52 am

jwei512 wrote:Uhh so I'm not entirely sure what you're code is trying to do...

For example... if you take a look at:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. EditText nameEdit = (EditText)findViewById(R.id.VoipUnEdit);
  2. nameEdit.setText(name);
  3.  
  4. // ...
  5.  
  6. final String nameE = nameEdit.getText().toString();
  7. submit_Button.setOnClickListener(new View.OnClickListener(){
  8.      public void onClick(View v){
  9.           Log.d("SETTINGS DATA", "NameEdit= " + nameE);
  10.           newSettings.set_Id("0");
  11.           updateSettings(2, newSettings);
  12.       }
  13. });
  14.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


So basically it seems like you are retrieving your EditText called nameEdit, which is fine. Then you set the text to be some String "name". Then you get back that same string with

final String nameE = nameEdit.getText().toString();

And so here, you will have nameE = name. Then nameE is declared final... so it will never change, and everytime you click your button you will just print it out.

I'm not sure what this "Settings" object is or what newSettings.set_Id does but if you're expecting the Log.d to print out something different each time you click your button - it's not.

Is this what's supposed to happen? If it's not, then retrieving the edit text content is easy.



Thanks for getting back to me,

Firstly, "newSettings.set_Id("0"); " is just in there to check something out and has no bearing on what I'm trying to do, I should have removed it before posting the code, sorry.

You have what I want to do otherwise basically correct.

I retrieve the EditText and then supply it with the info I want displayed in it - this works

So then the user can change the text in EditText and then the line

final String nameE = nameEdit.getText().toString();

should get this change in the EditText and display it.

It currently kind of works, the old text gets displayed when I click the button but if I exit and restart the app then the new text is there and gets displayed by the log on the press of the button.

I know its something simple in my code that needs changing to make the text change without the need to restart the app but Andoid UI really is a weak point of mine :oops:
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Postby jwei512 » Fri Jan 08, 2010 10:59 am

Hm okay so I think what you want to do is:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. // DON'T DO THIS
  3.  
  4. // final String nameE = nameEdit.getText().toString();
  5.  
  6.  
  7.  
  8. submit_Button.setOnClickListener(new View.OnClickListener(){
  9.  
  10.      public void onClick(View v){
  11.  
  12.           // Get the string once you actually click the button
  13.  
  14.           String nameE = nameEdit.getText().toString();
  15.  
  16.           Log.d("SETTINGS DATA", "NameEdit= " + nameE);
  17.  
  18.           newSettings.set_Id("0");
  19.  
  20.           updateSettings(2, newSettings);
  21.  
  22.       }
  23.  
  24. });
  25.  
  26.  
Parsed in 0.037 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 Draffodx » Fri Jan 08, 2010 11:21 am

Knew it was something simple :shock:

Thanks very much!
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests