Why this loop not go in else part of if condition?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • usergood
    New Member
    • Feb 2014
    • 1

    Why this loop not go in else part of if condition?

    I need to check that data in SQLite database is present of not? but when i Checking it loop is not go in else part of if condition? I need to know why? Is this right way to check values of Sqlite to your enterd value?

    Code:
    package com.user.registration;
    
    import java.util.List;
    
    import com.androidhive.androidsqlite.R;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    
    public class Login extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.loginpage);
          final EditText editText = (EditText)findViewById(R.id.editText2); 
           final EditText editText2 = (EditText)findViewById(R.id.editText1); 
           Button button = (Button)findViewById(R.id.button1);
           button.setOnClickListener(new OnClickListener() {
    		
    		@Override
    		public void onClick(View arg0) {
    			// TODO Auto-generated method stub
    		   String str1 = editText.getText().toString();
                String str2 = editText2.getText().toString();	
    		
            DatabaseHandler db = new DatabaseHandler(Login.this);
           /**
             * CRUD Operations
             * */
            // Inserting Contacts
       //  Log.d("Insert: ", "Inserting .."); 
            //db.getContact(3);
         System.out.print("Value of str1="+ str1);
            Log.d("Reading: ", "Reading all contacts..");
            List<Contact> contacts = db.getAllContacts();       
            System.out.print("Value of str1="+ str1);
            for (Contact cn : contacts) {
                String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
                    // Writing Contacts to log
            if(cn.getName().equals(str1) && cn.getPhoneNumber().equals(str2))
            {
            	Toast.makeText(Login.this,"You login successfully",Toast.LENGTH_LONG).show();
            
            }
            
            	else
            	{
            		Toast.makeText(Login.this,"Login Failed",Toast.LENGTH_LONG).show();
            		System.out.print("Value of str1="+ str1);
            	}
                Log.d("Name: ", log);
                System.out.print("Value of str1="+ str1);
            }
    		
    		}
           });
    	
        }
    }
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi usergood and welcome to bytes.com!

    DatabaseHandler is not a standard class but searching for it I found this tutorial which I'm guessing you're following, right?

    Generally speaking, I can't see anything wrong with your if-else-structure. My suggestion would be to put the following lines in front of the if and then check the output:
    [code=java]
    System.out.prin tln("str1 = '" + str1 + "', cn.getName() = '" + cn.getName() + "'.");
    System.out.prin tln("str2 = '" + str2 + "', cn.getPhoneNumb er() = '" + cn.getPhoneNumb er() + "'.");[/code] or alternatively [code=java]
    Log.d("str1 = '" + str1 + "', cn.getName() = '" + cn.getName() + "'.");
    Log.d("str2 = '" + str2 + "', cn.getPhoneNumb er() = '" + cn.getPhoneNumb er() + "'.");[/code]
    Chances are that they are equal even though you don't want them to be or that this is called at the wrong time.

    Comment

    Working...