mercredi 27 mai 2015

How to avoid redundancy in database?

I created an user registration & Log in app. I have done the following. MainActivity.java

 si=(Button)findViewById(R.id.button);
    su=(Button)findViewById(R.id.button2);
    si.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent1=new Intent(MainActivity.this,Login.class);
            startActivity(intent1);
        }
    });
    su.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent2=new Intent(MainActivity.this,Signup.class);
            startActivity(intent2);
        }
    });

Signup.java

databaseAdapter=new DatabaseAdapter(this);
    databaseAdapter=databaseAdapter.open();

    un=(EditText)findViewById(R.id.editText);
    pwd=(EditText)findViewById(R.id.editText2);
    cnfpwd=(EditText)findViewById(R.id.editText3);
    signu=(Button)findViewById(R.id.button3);

    signu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String Uname=un.getText().toString();
            String passw=pwd.getText().toString();
            String cnfpassw=cnfpwd.getText().toString();

            if (Uname.equals("")||passw.equals("")||cnfpassw.equals("")){
                //Toast.makeText(getApplicationContext(),"Please Fill all the Fields",Toast.LENGTH_SHORT).show();
                show("Error","Please fill all the fields");
                return;
            }

            if(!passw.equals(cnfpassw)){
                //Toast.makeText(getApplicationContext(),"Password does not match",Toast.LENGTH_SHORT).show();
                show("Error","Password does not match");
                pwd.setText("");
                cnfpwd.setText("");
                return;
            } else {
                databaseAdapter.insertEntry(Uname,passw);
                //Toast.makeText(getApplicationContext(),"Account Created Successfully",Toast.LENGTH_LONG).show();
                show("Success", "Account Created Successfully");
                //finish();
                un.setText("");
                pwd.setText("");
                cnfpwd.setText("");
            }
        }
    });
}

private void show(String title, String message) {
    AlertDialog.Builder builder=new AlertDialog.Builder(Signup.this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
}

Login.java

databaseAdapter=new DatabaseAdapter(this); databaseAdapter=databaseAdapter.open();

    UN=(EditText)findViewById(R.id.editText4);
    PWD=(EditText)findViewById(R.id.editText5);
    si=(Button)findViewById(R.id.button4);

    si.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String UserName=UN.getText().toString();
            String Password=PWD.getText().toString();

            String storedPassword=databaseAdapter.getSingleEntry(UserName);
            //String storesUserName=databaseAdapter.getUserName(Password);

            if (UserName.equals("")||Password.equals("")){
                show("Error","Please fill all Fields");
                return;
            }
            if (Password.equals(storedPassword)){
                //Toast.makeText(Login.this,"Login Successful",Toast.LENGTH_SHORT).show();
                show("Success","Login Successful");
                String storedUserName=databaseAdapter.getUserDetails(Password);
                Toast.makeText(getApplicationContext(),storedUserName,Toast.LENGTH_SHORT).show();
                Bundle bundle=new Bundle();
                bundle.putString("number",storedUserName);
                Intent intent2=new Intent(Login.this,Inbox.class);
                intent2.putExtras(bundle);
                startActivity(intent2);
            } else {
                //Toast.makeText(getApplicationContext(),"Username or Password is incorrect",Toast.LENGTH_SHORT).show();
                show("Error","Username or Password is incorrect");
                UN.setText("");
                PWD.setText("");
            }
        }
    });
}

private void show(String title, String message) {
    AlertDialog.Builder builder=new AlertDialog.Builder(Login.this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
}

Now i can Signup and Log in from database. But the Database allows redundancy. i.e., it allows the same value any number of times. I want to avoid that in order to register unique Usernames. Any Solutions?

Aucun commentaire:

Enregistrer un commentaire