• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps EditText to string and logic problems

ninjasenses

Newbie
Feb 9, 2010
38
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:
package com.cutter.giddyup;

import java.util.Map;

import android.R.bool;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Home extends Activity {
	 public static final String PREFS = "Preferences";
	 private String AUTHENTICATED=""; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button Setup = (Button)findViewById(R.id.setup);
        //Check to see if user is authenticated
        if(isAuthenticated()){

            Setup.setOnClickListener(new OnClickListener() {
            	
            	public void onClick(View v) {
            		Intent SetupIntent = new Intent(Home.this,Setup.class);
            		startActivity(SetupIntent);
            	}
            });
        	
        }
        else{
    		Intent SetupIntent = new Intent(Home.this,Setup.class);
    		startActivity(SetupIntent);
        }

        

    }
    public boolean isAuthenticated(){
        SharedPreferences settings = getSharedPreferences(PREFS, 0);
        
        if (settings.contains(AUTHENTICATED)) { 
        		                        String secret = settings.getString(AUTHENTICATED, ""); 
        		                        if(!(secret.equals(""))) { 
        		                                return true;
        		                        } 
        		                        else{
        		                        	return false;
        		                        }
        		                        
        }
        else{
        	return false;
        }
		
    }
}

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:
package com.cutter.giddyup;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.http.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;

import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
//apache

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;


public class Setup extends Activity{
	 public static final String PREFS = "Preferences";
	 private String AUTHENTICATED=""; 
	 private String USER="";
	 private String PASSWORD="";
	 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setup);  

        EditText username = (EditText)findViewById(R.id.username);
        EditText password = (EditText)findViewById(R.id.password);
        
        SharedPreferences settings = getSharedPreferences(PREFS, 
        		0); 
        String un = settings.getString(USER, "").toString();
        String pw = settings.getString(PASSWORD, "").toString();
        
        username.setText(un);
        password.setText(pw);
        

        Button Save = (Button)findViewById(R.id.saveLogin);
        Save.setOnClickListener(new OnClickListener() {
        	
        	public void onClick(View v) {
        			postData();
        			//write to prefs if authenticated
        	        EditText username1 = (EditText)findViewById(R.id.username);
        	        EditText password2 = (EditText)findViewById(R.id.password);
        	        AUTHENTICATED="true";
        	        
                    SharedPreferences settings = getSharedPreferences(PREFS, 
                    		0); 
                    		            SharedPreferences.Editor editor = settings.edit(); 
                    		            editor.putString(AUTHENTICATED, "true");
                    		            editor.putString(USER, username1.getText().toString());
                    		            editor.putString(PASSWORD, password2.getText().toString());
                    		            editor.commit(); 
        	}
        });
        
        Button Clear = (Button)findViewById(R.id.clearLogin);
        Clear.setOnClickListener(new OnClickListener() {
        	
        	public void onClick(View v) {
        			clearData();
        	}
        });
        

    }
    public void postData() {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://localhost/Android/Login.ashx");

        try {
            EditText username = (EditText)findViewById(R.id.username);
            EditText password = (EditText)findViewById(R.id.password);
            
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("un", username.toString()));
            nameValuePairs.add(new BasicNameValuePair("pw", password.toString()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    } 
    public void clearData(){
        EditText username = (EditText)findViewById(R.id.username);
        EditText password = (EditText)findViewById(R.id.password);
        
        username.setText("");
        password.setText("");
    }
}

any help would be greatly appreciated. Thanks
 
Try setting your SharedPreferences keys to something besides empty Strings. Like:
Code:
private static final AUTHENTICATED = "authenticated";
private static final USER="user";
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
 
Upvote 0
Try setting your SharedPreferences keys to something besides empty Strings. Like:
Code:
private static final AUTHENTICATED = "authenticated";
private static final USER="user";
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

That was it!! Thanks so much
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones