0

I am having problem in a simple Login-signup app. I just started programming in android. My problem is that, sign up button has no effect on clicking. I don't know whether the problem is of if else or database.

signup.java

    public class signup extends Activity{
    Button btn;
    EditText edt1,edt2,edt3,edt4,edt5,edt6,edt7,edt8;
    SQLiteDatabase mydb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);
        edt1=(EditText)findViewById(R.id.nameedit);
        edt2=(EditText)findViewById(R.id.addrsedit);
        edt3=(EditText)findViewById(R.id.cityedit);
        edt4=(EditText)findViewById(R.id.pincodedit);
        edt5=(EditText)findViewById(R.id.usrnmeedit);
        edt6=(EditText)findViewById(R.id.passsgnedit);
        edt7=(EditText)findViewById(R.id.mobedit);
        edt8=(EditText)findViewById(R.id.emledit);
        btn=(Button)findViewById(R.id.sgnup);
        mydb=this.openOrCreateDatabase("shopping",MODE_PRIVATE,null);
        mydb.execSQL("CREATE TABLE IF NOT EXISTS contacts(name varchar,adrs varchar,city varchar,pin varchar,uname varchar,pass varchar,mob varchar,eid varchar)");

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String nm=edt1.getText().toString().trim();
                String ad=edt2.getText().toString().trim();
                String cty=edt3.getText().toString().trim();
                String pin=edt4.getText().toString().trim();
                String usr=edt5.getText().toString().trim();
                String pwd=edt6.getText().toString().trim();
                String mbl=edt7.getText().toString().trim();
                String eml=edt8.getText().toString().trim();            
                Cursor cur=mydb.rawQuery("select * from contacts",null);
                while (cur.moveToNext()) 
                {
                String un=cur.getString(cur.getColumnIndex("uname"));
                String emailPattern="[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";


                if (un.equals(usr))

                    Toast.makeText(signup.this,"username already exits,try another",Toast.LENGTH_SHORT).show(); 


                else{
                         if (eml.matches(emailPattern))
                         {

                            mydb.execSQL("INSERT INTO contacts VALUES('"+nm+"','"+ad+"','"+cty+"','"+pin+"','"+usr+"','"+pwd+"','"+mbl+"','"+eml+"')");
                            Toast.makeText(signup.this, "submitted successfully",500).show();
                            Intent in=new Intent(signup.this,login.class);
                            startActivity(in);
                         }
                         else 
                        {
                            Toast.makeText(getApplicationContext(),"Invalid email address",Toast.LENGTH_SHORT).show();
                        }   
            }
                }

                }

        });

    }

}

EDITED

Cursor cur=mydb.rawQuery("select * from contacts",null);


            Boolean found=false;

            while (cur.moveToNext()) 
            {
                found=true;

            String un=cur.getString(cur.getColumnIndex("uname"));




            if (un.equals(usr))

                Toast.makeText(signup.this,"username already exits,try another",Toast.LENGTH_SHORT).show(); 


            else{
                     if (eml.matches(emailPattern))
                     {

                        mydb.execSQL("INSERT INTO contacts VALUES('"+nm+"','"+ad+"','"+cty+"','"+pin+"','"+usr+"','"+pwd+"','"+mbl+"','"+eml+"')");
                        Toast.makeText(signup.this, "submitted successfully",500).show();
                        Intent in=new Intent(signup.this,MainActivity.class);
                        startActivity(in);
                     }
                     else 
                    {
                        Toast.makeText(getApplicationContext(),"Invalid email address",Toast.LENGTH_SHORT).show();
                    }   

            }
            break;
            }
            if (found) 
                return;
panda_heart_3
  • 227
  • 2
  • 17

1 Answers1

1

I think initially your query does not return any result. Which may mean your table is empty.

Cursor cur=mydb.rawQuery("select * from contacts",null);
// if table is empty the while loop will fail
while (cur.moveToNext()) 

To test your code, insert some data in your contacts table first.

Try this:

           boolean found = false;
            while(cur.moveToNext()) 
            {
               ... your code like before
               // if found save data and break
               found = true;
               break;
            }

            if(found) return;

                if (eml.matches(emailPattern))
                     {

                        mydb.execSQL("INSERT INTO contacts VALUES('"+nm+"','"+ad+"','"+cty+"','"+pin+"','"+usr+"','"+pwd+"','"+mbl+"','"+eml+"')");
                        Toast.makeText(signup.this, "submitted successfully",500).show();
                        Intent in=new Intent(signup.this,login.class);
                        startActivity(in);
                     }
                     else 
                    {
                        Toast.makeText(getApplicationContext(),"Invalid email address",Toast.LENGTH_SHORT).show();
                    }   

            }
ZakiMak
  • 2,072
  • 2
  • 17
  • 26