EditText to string and logic problems


Last Updated:

  1. ninjasenses

    ninjasenses Active Member This Topic's Starter

    Joined:
    Feb 9, 2010
    Messages:
    38
    Likes Received:
    0
    Hey guys...I have some weird things going on with my android app. The first thing is a simple logic problem. I have some thing evaluating to true but it is not being detected correctly...here is my code:

    Code (Text):
    1.  
    2. package com.cutter.giddyup;
    3.  
    4. import java.util.Map;
    5.  
    6. import android.R.bool;
    7. import android.app.Activity;
    8. import android.content.Intent;
    9. import android.content.SharedPreferences;
    10. import android.os.Bundle;
    11. import android.view.View;
    12. import android.view.View.OnClickListener;
    13. import android.widget.Button;
    14.  
    15. public class Home extends Activity {
    16.      public static final String PREFS = "Preferences";
    17.      private String AUTHENTICATED="";
    18.     /** Called when the activity is first created. */
    19.     @Override
    20.     public void onCreate(Bundle savedInstanceState) {
    21.         super.onCreate(savedInstanceState);
    22.         setContentView(R.layout.main);
    23.  
    24.         Button Setup = (Button)findViewById(R.id.setup);
    25.         //Check to see if user is authenticated
    26.         if(isAuthenticated()){
    27.  
    28.             Setup.setOnClickListener(new OnClickListener() {
    29.                
    30.                 public void onClick(View v) {
    31.                     Intent SetupIntent = new Intent(Home.this,Setup.class);
    32.                     startActivity(SetupIntent);
    33.                 }
    34.             });
    35.            
    36.         }
    37.         else{
    38.             Intent SetupIntent = new Intent(Home.this,Setup.class);
    39.             startActivity(SetupIntent);
    40.         }
    41.  
    42.        
    43.  
    44.     }
    45.     public boolean isAuthenticated(){
    46.         SharedPreferences settings = getSharedPreferences(PREFS, 0);
    47.        
    48.         if (settings.contains(AUTHENTICATED)) {
    49.                                         String secret = settings.getString(AUTHENTICATED, "");
    50.                                         if(!(secret.equals(""))) {
    51.                                                 return true;
    52.                                         }
    53.                                         else{
    54.                                             return false;
    55.                                         }
    56.                                        
    57.         }
    58.         else{
    59.             return false;
    60.         }
    61.        
    62.     }
    63. }
    64.  
    The problem here is at the line with if (settings.contains(AUTHENTICATED))...it is returning true when I am debugging, but it keeps going to the else{return false} for some reason. I don't get it?

    The second problem is when Im trying to store text from an EditText control(ie:edittext value = android.widget.EditText@43bca3d8a). I am trying to store a simple username and password a person enters, but it keeps storing that weird value. When I evalute username1.getText().toString()...it grabs the correct text...but when I retrieve it I get the weird value. here is my code:


    Code (Text):
    1. package com.cutter.giddyup;
    2. import android.os.Bundle;
    3. import android.view.View;
    4. import android.view.View.OnClickListener;
    5. import android.widget.Button;
    6. import android.widget.EditText;
    7. import android.widget.TextView;
    8. import android.R.string;
    9. import android.app.Activity;
    10. import android.content.Intent;
    11. import android.content.SharedPreferences;
    12. import android.net.http.*;
    13. import org.apache.http.HttpEntity;
    14. import org.apache.http.HttpResponse;
    15. import org.apache.http.NameValuePair;
    16.  
    17. import java.io.IOException;
    18. import java.util.List;
    19. import java.util.ArrayList;
    20. //apache
    21.  
    22. import org.apache.http.client.ClientProtocolException;
    23. import org.apache.http.client.HttpClient;
    24. import org.apache.http.client.entity.UrlEncodedFormEntity;
    25. import org.apache.http.client.methods.HttpPost;
    26. import org.apache.http.impl.client.DefaultHttpClient;
    27. import org.apache.http.message.BasicNameValuePair;
    28. import org.apache.http.params.HttpParams;
    29.  
    30.  
    31. public class Setup extends Activity{
    32.      public static final String PREFS = "Preferences";
    33.      private String AUTHENTICATED="";
    34.      private String USER="";
    35.      private String PASSWORD="";
    36.      
    37.     public void onCreate(Bundle savedInstanceState) {
    38.         super.onCreate(savedInstanceState);
    39.         setContentView(R.layout.setup);  
    40.  
    41.         EditText username = (EditText)findViewById(R.id.username);
    42.         EditText password = (EditText)findViewById(R.id.password);
    43.        
    44.         SharedPreferences settings = getSharedPreferences(PREFS,
    45.                 0);
    46.         String un = settings.getString(USER, "").toString();
    47.         String pw = settings.getString(PASSWORD, "").toString();
    48.        
    49.         username.setText(un);
    50.         password.setText(pw);
    51.        
    52.  
    53.         Button Save = (Button)findViewById(R.id.saveLogin);
    54.         Save.setOnClickListener(new OnClickListener() {
    55.            
    56.             public void onClick(View v) {
    57.                     postData();
    58.                     //write to prefs if authenticated
    59.                     EditText username1 = (EditText)findViewById(R.id.username);
    60.                     EditText password2 = (EditText)findViewById(R.id.password);
    61.                     AUTHENTICATED="true";
    62.                    
    63.                     SharedPreferences settings = getSharedPreferences(PREFS,
    64.                             0);
    65.                                         SharedPreferences.Editor editor = settings.edit();
    66.                                         editor.putString(AUTHENTICATED, "true");
    67.                                         editor.putString(USER, username1.getText().toString());
    68.                                         editor.putString(PASSWORD, password2.getText().toString());
    69.                                         editor.commit();
    70.             }
    71.         });
    72.        
    73.         Button Clear = (Button)findViewById(R.id.clearLogin);
    74.         Clear.setOnClickListener(new OnClickListener() {
    75.            
    76.             public void onClick(View v) {
    77.                     clearData();
    78.             }
    79.         });
    80.        
    81.  
    82.     }
    83.     public void postData() {
    84.         // Create a new HttpClient and Post Header
    85.         HttpClient httpclient = new DefaultHttpClient();
    86.         HttpPost httppost = new HttpPost("http://localhost/Android/Login.ashx");
    87.  
    88.         try {
    89.             EditText username = (EditText)findViewById(R.id.username);
    90.             EditText password = (EditText)findViewById(R.id.password);
    91.            
    92.             // Add your data
    93.             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    94.             nameValuePairs.add(new BasicNameValuePair("un", username.toString()));
    95.             nameValuePairs.add(new BasicNameValuePair("pw", password.toString()));
    96.             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    97.  
    98.             // Execute HTTP Post Request
    99.             HttpResponse response = httpclient.execute(httppost);
    100.  
    101.            
    102.         } catch (ClientProtocolException e) {
    103.             // TODO Auto-generated catch block
    104.         } catch (IOException e) {
    105.             // TODO Auto-generated catch block
    106.         }
    107.     }
    108.     public void clearData(){
    109.         EditText username = (EditText)findViewById(R.id.username);
    110.         EditText password = (EditText)findViewById(R.id.password);
    111.        
    112.         username.setText("");
    113.         password.setText("");
    114.     }
    115. }
    116.  
    any help would be greatly appreciated. Thanks
     

    Advertisement
  2. Boogs

    Boogs Well-Known Member

    Joined:
    Apr 6, 2010
    Messages:
    85
    Likes Received:
    16
    Try setting your SharedPreferences keys to something besides empty Strings. Like:
    Code (Text):
    1. private static final AUTHENTICATED = "authenticated";
    2. private static final USER="user";
    3. private static final PASSWORD="password";
    I'm assuming that your SharedPreferences object doesn't like you using empty strings as keys, and using the same key (empty string) for each preference. Dollars to doughnuts that that's your problem.

    --Boogs
     
  3. ninjasenses

    ninjasenses Active Member This Topic's Starter

    Joined:
    Feb 9, 2010
    Messages:
    38
    Likes Received:
    0
    That was it!! Thanks so much
     

Share This Page

Loading...