mercredi 4 mai 2016

Issues in Database

Minor issues in my project but i cant understand that errors are 1:when i am login with database data it don't display it display only last row of cursor data and it display all database data i want particular register data only 2:when i am comparing email pattern it display error unfortunately iwt login stopped.can any one solve this error?

LoginActivity

 public void loginUser(View v)
     {


    username1 = et.getText().toString();
    password1= et1.getText().toString();
    String username="",password="";
        try
        {
            StringBuffer sb = new StringBuffer();
            String qry = "select username,password from user ";
            Cursor c= db.rawQuery(qry,null);
            boolean b = c.moveToFirst();
            if (b)
            {
                do {
                    username = c.getString(0);
                    password = c.getString(1);
                    sb.append(username+"-"+password+"\n");
                }while(c.moveToNext());
            }
            if (username.equals(username1)&&password.equals(password1))
            {
                String data = sb.toString();
                Intent i1 = new Intent(this,WelcomeActivity.class);
                i1.putExtra("k1",data);
                startActivity(i1);
            }
            else {
                Toast.makeText(MainActivity.this, "No data to display", Toast.LENGTH_SHORT).show();
            }

        }catch (Exception e)
        {
            Log.e("TAG: ReadDataException","",e);
        }
}

Signup Activity

EditText ett[]= new EditText[4];


int[] items = {R.id.username1,R.id.password1,R.id.email,R.id.phone};
String values[] = new String[4];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signup_layout);
    for (int i = 0; i<ett.length;i++)
    {
        ett[i] = (EditText)findViewById(items[i]);
    }
}
public void registerUser(View v)
{
    String emailpattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    for (i = 0;i<ett.length;i++)
    {
        values[i] = ett[i].getText().toString();
        if (values[i].equals(""))
        {
            ett[i].setError("Fields is Empty");
            break;
        }
      /*else if (!values[2].matches(emailpattern))
        {
            ett[2].setError("Enter valid email charcters");
        }*/
    }
    if (i==ett.length)
    {
        int phonenumber = Integer.parseInt(values[3]);
        String qry = "insert into user values('"+values[0]+"','"+values[1]+"','"+values[2]+"',"+phonenumber+")";
        try
        {
            SQLiteDatabase db = MainActivity.mydb.getWritableDatabase();
            db.execSQL(qry);
            AlertDialog.Builder ab = new AlertDialog.Builder(this);
            ab.setMessage("Successfully Registered");
            ab.show();
            this.finish();
            Intent i1 = new Intent(this,MainActivity.class);
            startActivity(i1);
        }catch (Exception e)
        {
            Log.e("TAG: InsertionException","",e);
            Toast.makeText(SignupActivity.this, "not inserted", Toast.LENGTH_SHORT).show();
        }
    }
    else
    {
        Toast.makeText(SignupActivity.this, "Fill data", Toast.LENGTH_SHORT).show();
    }
}

Aucun commentaire:

Enregistrer un commentaire