Sunday, October 14, 2018

JANITH DBHELPER

Listview

public class DBhelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME="user-Info1.db";
    public DBhelper( Context context) {
        super(context, DATABASE_NAME, null, 2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String SQL_CREATE_USER="CREATE TABLE "+Table_Name+
                "("+COLUMN_NAME_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
                +COLUMN_NAME_USERNAME+" TEXT,"
                +COLUMN_NAME_DATEOfBIRTH+" TEXT,"
                +COLUMN_NAME_GENDER+" TEXT,"
                +COLUMN_NAME_PASSWORD+" TEXT)";
        db.execSQL(SQL_CREATE_USER);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    public long checkUser(String username, String password) {
        SQLiteDatabase db = getReadableDatabase();

        String[] selectionArgs = {username, password};
        String query = "SELECT * FROM " + Table_Name + " WHERE "+ COLUMN_NAME_USERNAME + " = ? AND " +
                COLUMN_NAME_PASSWORD + " = ?";

        Cursor cursor = db.rawQuery(query, selectionArgs);
        long userId = -1;
        if (cursor.moveToNext()) {
            userId = cursor.getLong(0);
        }
        return userId;
    }
    public long addinfo(String username,String DOB,String gender,String password)
    {
        SQLiteDatabase db=getWritableDatabase();

        ContentValues values=new ContentValues();
        values.put(COLUMN_NAME_USERNAME,username);
        values.put(COLUMN_NAME_DATEOfBIRTH,DOB);
        values.put(COLUMN_NAME_GENDER,gender);
        values.put(COLUMN_NAME_PASSWORD,password);
        long newrecode=db.insert(Table_Name,null,values);
        return  newrecode;
    }

    public boolean updateInfor(String username,String DOB,String gender,String password,String ID)
    {
        SQLiteDatabase db=getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(COLUMN_NAME_USERNAME,username);
        values.put(COLUMN_NAME_DATEOfBIRTH,DOB);
        values.put(COLUMN_NAME_GENDER,gender);
        values.put(COLUMN_NAME_PASSWORD,password);

        String selection=COLUMN_NAME_ID+" LIKE ?";
        String[] selectargs={ID};

        int count=db.update(Table_Name,values,selection,selectargs);

        if(count==0)
        {
            return false;
        }

        else
        {
            return true;
        }
    }

public Cursor readAllInfor()
{
    SQLiteDatabase db=getReadableDatabase();
    String[] projection={
            COLUMN_NAME_ID,
            COLUMN_NAME_USERNAME,
            COLUMN_NAME_PASSWORD,
            COLUMN_NAME_DATEOfBIRTH,
            COLUMN_NAME_GENDER};

    String sortOder=COLUMN_NAME_ID+" DESC";

    Cursor cursor=db.query(Table_Name,
                    projection,
                    null,
                    null,
                    null,
                    null,
                    sortOder);


    return cursor;
}
    public Cursor readAllInfor(String ID)
    {
        SQLiteDatabase db=getReadableDatabase();
        String[] projection={
                COLUMN_NAME_ID,
                COLUMN_NAME_USERNAME,
                COLUMN_NAME_PASSWORD,
                COLUMN_NAME_DATEOfBIRTH,
                COLUMN_NAME_GENDER};

        String selection=COLUMN_NAME_ID+"= ?";
        String[] selectargs={ID};
        Cursor cursor=db.query(Table_Name,
                projection,
                selection,
        selectargs,
        null,
        null,
        null);

        return cursor;
        }
public int deleteInfo(String ID)
        {
        SQLiteDatabase db=getWritableDatabase();
        String selection=COLUMN_NAME_ID+" LIKE ?";
        String[] selectargs={ID};
        int count=db.delete(Table_Name,selection,selectargs);
        return count;

        }
        }

//Profile MGT

public class ProfileManagement extends AppCompatActivity {

     EditText userName,DOB,userpassword;
    private RadioGroup radioGroup;
    private RadioButton radioButton;
     Button Register;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_management);

        Register=findViewById(R.id.Register);
        userName=findViewById(R.id.InUsernamePro);
        DOB=findViewById(R.id.InDBOPRo);
        userpassword=findViewById(R.id.InPasswordPro);


        Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String UserName,userpaass,userDBO,genderselect;

                UserName=userName.getText().toString();
                userDBO=DOB.getText().toString();
                userpaass=userpassword.getText().toString();

                radioGroup =  findViewById(R.id.genderPro);
                // get selected radio button from radioGroup
                int selectedId = radioGroup.getCheckedRadioButtonId();
                // find the radiobutton by returned id
                radioButton =findViewById(selectedId);
                genderselect=radioButton.getText().toString();

                DBhelper dBhelper = new DBhelper(ProfileManagement.this);
                long addn = dBhelper.addinfo(UserName, userDBO, genderselect, userpaass);

                if(addn>0) {
                    Toast.makeText(ProfileManagement.this, "Insert data successful", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(ProfileManagement.this, "Not insert data", Toast.LENGTH_LONG).show();
                }

            }
        });

    }
}

//HOME


public class Home extends AppCompatActivity {

    EditText usernameChek,passwordChek;
    Button login1,register1;
    DBhelper dBhelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        dBhelper = new DBhelper(Home.this);
        login1=findViewById(R.id.login);
        register1=findViewById(R.id.register);
        usernameChek=findViewById(R.id.Username);
        passwordChek=findViewById(R.id.Password);

        login1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String usernameChekString,passwordChekString;
                usernameChekString=usernameChek.getText().toString();
                passwordChekString=passwordChek.getText().toString();
               long chek= dBhelper.checkUser(usernameChekString,passwordChekString);
                if(chek>-1)
                {
                    startActivity(new Intent(Home.this,EditProfile.class));
                }
                else
                {
                    Toast.makeText(Home.this, "Login not successful", Toast.LENGTH_SHORT).show();
                }


            }
        });

        register1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Home.this,ProfileManagement.class);
                startActivity(intent);


            }
        });
    }
}

//EDIT Profile

public class EditProfile extends AppCompatActivity {
    private RadioGroup radioGroup;
    String genderEditeProfile=null;
    Button Editeprofile,searchBtn,deleteuser;
    EditText searchIDIN,bithdaySET,passwordSET,usernameSET;
    private RadioButton radioButton;
    DBhelper dBhelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

         dBhelper = new DBhelper(EditProfile.this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_profile);


        searchIDIN=findViewById(R.id.searchID);
        usernameSET=findViewById(R.id.username);
        bithdaySET=findViewById(R.id.bithday);
        passwordSET=findViewById(R.id.password);
        deleteuser=findViewById(R.id.Deleteuser);

        radioGroup =  findViewById(R.id.genderEdit);
        searchBtn=findViewById(R.id.serch);
        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String idsearch=searchIDIN.getText().toString();
                if (idsearch.isEmpty()){
                    Toast.makeText(EditProfile.this,"Please enter a ID",Toast.LENGTH_SHORT).show();
                }
                else {
                    Cursor cursor=dBhelper.readAllInfor(idsearch);
                    if(cursor.getCount()==0){
                        usernameSET.setText(" ");
                        passwordSET.setText(" ");
                        bithdaySET.setText(" ");
                        radioGroup.clearCheck();
                        Toast.makeText(EditProfile.this,"Please enter a valid ID",Toast.LENGTH_SHORT).show();

                    }
                    else {
                        while (cursor.moveToNext()) {
                            usernameSET.setText(cursor.getString(1));
                            passwordSET.setText(cursor.getString(2));
                            bithdaySET.setText(cursor.getString(3));
                            if (cursor.getString(4).equals("Male")) {
                                radioGroup.check(R.id.male);
                            } else if (cursor.getString(4).equals("Female"))
                                radioGroup.check(R.id.female);
                        }
                    }

                }
                }
        });
        Editeprofile=findViewById(R.id.update);
        Editeprofile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String UserName,userpaass,userDBO,genderselect;

                String IDUPDATE=searchIDIN.getText().toString();
                UserName=usernameSET.getText().toString();
                userDBO=bithdaySET.getText().toString();
                userpaass=passwordSET.getText().toString();

                // get selected radio button from radioGroup
                int selectedId = radioGroup.getCheckedRadioButtonId();
                // find the radiobutton by returned id

                radioButton =findViewById(selectedId);
                genderselect=radioButton.getText().toString();

                DBhelper dBhelper = new DBhelper(EditProfile.this);
                boolean addn = dBhelper.updateInfor(UserName, userDBO, genderselect, userpaass,IDUPDATE);

                if(addn==true) {
                    Toast.makeText(EditProfile.this, "Update data", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(EditProfile.this, "Not update data", Toast.LENGTH_LONG).show();
                }

            }
        });
        deleteuser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String delete=searchIDIN.getText().toString();
                DBhelper dBhelper = new DBhelper(EditProfile.this);
                int donedelete =dBhelper.deleteInfo(delete);
                if(donedelete==0){
                    Toast.makeText(EditProfile.this, "Not Delete", Toast.LENGTH_LONG).show();

                }
                else{
                    usernameSET.setText(" ");
                    passwordSET.setText(" ");
                    bithdaySET.setText(" ");
                    radioGroup.clearCheck();
                    Toast.makeText(EditProfile.this, "Done Delete", Toast.LENGTH_LONG).show();
                }
            }
        });
    }



}






No comments:

Post a Comment

JANITH DBHELPER Listview public class DBhelper extends SQLiteOpenHelper {     public static final String DATABASE_NAME="user-Info1...