Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DataBase;
- import android.provider.BaseColumns;
- public class UserProfile{
- private UserProfile(){
- }
- public class User implements BaseColumns {
- public static final String TABLE = "UserInfo";
- public static final String Profile_ID_Col = "ID";
- public static final String Prof_Name_Col = "userName";
- public static final String DOB_Col = "dateOfBirth";
- public static final String gender_Col = "Gender";
- public static final String password_col = "Password";
- }
- }
- package DataBase;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.widget.EditText;
- public class DBHelper extends SQLiteOpenHelper {
- public static final int DATABASE_VERSION = 1;
- public static final String DATABASE_NAME = "UserProfile.db";
- private SQLiteDatabase DB;
- private Cursor RESULTS;
- private ContentValues VALUES;
- public DBHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE IF NOT EXISTS " +UserProfile.User.TABLE +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,userName TEXT,dateOfBirth TEXT,Gender TEXT,Password TEXT)");
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS "+UserProfile.User.TABLE);
- onCreate(db);
- }
- public boolean AddInfo(String Name,String dob,String gender, String password ){
- VALUES = new ContentValues();
- DB = this.getWritableDatabase();
- VALUES.put(UserProfile.User.Prof_Name_Col,Name);
- VALUES.put(UserProfile.User.DOB_Col,dob);
- VALUES.put(UserProfile.User.gender_Col,gender);
- VALUES.put(UserProfile.User.password_col,password);
- long r = DB.insert(UserProfile.User.TABLE,null,VALUES);
- if (r == -1){
- return false;
- }
- else {
- return true;
- }
- }
- public Boolean UpdateInfo(String id,String Name,String DOB,String Gender,String password ){
- DB = this.getWritableDatabase();
- VALUES = new ContentValues();
- VALUES.put(UserProfile.User.Prof_Name_Col,Name);
- VALUES.put(UserProfile.User.DOB_Col,DOB);
- VALUES.put(UserProfile.User.gender_Col,Gender);
- VALUES.put(UserProfile.User.password_col,password);
- DB.update(UserProfile.User.TABLE,VALUES,"ID = ?",new String[]{id});
- return true;
- }
- public Cursor readAllInfor(String id){
- DB = this.getWritableDatabase();
- RESULTS = DB.rawQuery("SELECT * from " +UserProfile.User.TABLE + " WHERE TRIM(ID) = ? ", new String[]{id});
- return RESULTS;
- }
- public Cursor all(){
- DB = this.getWritableDatabase();
- RESULTS = DB.rawQuery("SELECT * FROM "+UserProfile.User.TABLE,null);
- return RESULTS;
- }
- public Integer deleteInfo(String id){
- DB = this.getWritableDatabase();
- return DB.delete(UserProfile.User.TABLE,"ID = ?",new String[]{id});
- }
- }
- import android.database.Cursor;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- import DataBase.DBHelper;
- import DataBase.UserProfile;
- public class EditProfile extends AppCompatActivity {
- private DBHelper DB;
- private Button Edit,Viewall;
- private Button Delete;
- private Button Search;
- private EditText Username,DOB,Password,id;
- private RadioGroup gender;
- private RadioButton selected;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_edit_profile);
- DB = new DBHelper(this);
- id = (EditText)findViewById(R.id.ID);
- Username = (EditText)findViewById(R.id.Usernametext);
- DOB = (EditText)findViewById(R.id.DOBteED);
- Password = (EditText)findViewById(R.id.PasswordTeed);
- gender = (RadioGroup)findViewById(R.id.radio);
- Edit = (Button)findViewById(R.id.editb);
- Delete = (Button)findViewById(R.id.deleteb);
- Search = (Button)findViewById(R.id.searchb);
- Viewall = (Button)findViewById(R.id.viewAll);
- DeleteData();
- UpdateData();
- search();
- viewDATA();
- }
- public void DeleteData(){
- Delete.setOnClickListener(
- new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Integer delete = DB.deleteInfo(id.getText().toString());
- if(delete > 0){
- Toast.makeText(EditProfile.this,"Data deleted",Toast.LENGTH_LONG).show();
- }
- else{
- Toast.makeText(EditProfile.this,"Data isn't Deleted",Toast.LENGTH_LONG).show();
- }
- }
- }
- );
- }
- public void UpdateData() {
- Edit.setOnClickListener(
- new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- selected = (RadioButton)findViewById(gender.getCheckedRadioButtonId());
- boolean isUpdate = DB.UpdateInfo(id.getText().toString(),Username.getText().toString(),DOB.getText().toString(), selected.getText().toString() ,Password.getText().toString());
- if(isUpdate == true)
- Toast.makeText(EditProfile.this,"Data is Updated",Toast.LENGTH_LONG).show();
- else
- Toast.makeText(EditProfile.this,"Data isn't Updated",Toast.LENGTH_LONG).show();
- }
- }
- );
- }
- public void search(){
- Search.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Cursor res = DB.readAllInfor(id.getText().toString());
- res.moveToFirst();
- String name = res.getString(res.getColumnIndex(UserProfile.User.Prof_Name_Col));
- String dob = res.getString(res.getColumnIndex(UserProfile.User.DOB_Col));
- String gend = res.getString(res.getColumnIndexOrThrow(UserProfile.User.gender_Col));
- String pass = res.getColumnName(res.getColumnIndex(UserProfile.User.password_col));
- if(!res.isClosed()){
- res.close();
- }
- Username.setText((CharSequence)name);
- DOB.setText((CharSequence)dob);
- if(gend == "Male"){
- RadioButton male = (RadioButton)findViewById(R.id.Male);
- male.setChecked(true);
- }
- else if(gend == "Female"){
- RadioButton female = (RadioButton)findViewById(R.id.Female);
- female.setChecked(true);
- }
- Password.setText((CharSequence)pass);
- }
- });
- }
- public void viewDATA(){
- Viewall.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Cursor Vieww = DB.all();
- if(Vieww.getCount() == 0){
- Toast.makeText(EditProfile.this,"Found nothing",Toast.LENGTH_LONG).show();
- return;
- }
- StringBuffer buffer = new StringBuffer();
- while (Vieww.moveToNext()){
- buffer.append("ID : "+Vieww.getString(0)+"n");
- buffer.append("Username : "+Vieww.getString(1)+"n");
- buffer.append("Date of birth : "+Vieww.getString(2)+"n");
- buffer.append("Gender : "+Vieww.getString(3)+"n");
- buffer.append("Password : "+Vieww.getString(4)+"nnn");
- }
- showMessage("Data",buffer.toString());
- }
- });
- }
- public void showMessage(String title,String Message) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setCancelable(true);
- builder.setTitle(title);
- builder.setMessage(Message);
- builder.show();
- }
- }
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import DataBase.DBHelper;
- public class Home extends AppCompatActivity {
- private EditText Username,Password;
- private Button register;
- private DBHelper DB1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_home);
- DB1 = new DBHelper(this);
- Username = (EditText)findViewById(R.id.UsernameHomeTe);
- Password = (EditText)findViewById(R.id.PasswordHomeTe);
- register = (Button)findViewById(R.id.registerBu);
- register.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- OpenActivity();
- }
- });
- }
- public void OpenActivity(){
- Intent intent = new Intent(this,ProfileManagement.class);
- startActivity(intent);
- }
- }
- import android.content.Intent;
- import android.database.Cursor;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- import DataBase.DBHelper;
- public class ProfileManagement extends AppCompatActivity {
- private DBHelper DB;
- private Button Updateb;
- private EditText Username,DOB,Password,id;
- private RadioGroup gender;
- private RadioButton selected;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_profile_management);
- DB = new DBHelper(this);
- id = (EditText)findViewById(R.id.ID);
- Username = (EditText)findViewById(R.id.UsernameprofEd);
- DOB = (EditText)findViewById(R.id.DOBtepro);
- gender = (RadioGroup)findViewById(R.id.radioo);
- Password = (EditText)findViewById(R.id.PasswordTepro);
- Updateb = (Button)findViewById(R.id.Updatebu);
- Updateb.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- System.out.println("///////////////////////////////////////////////////////////////////////////////");
- selected = (RadioButton)findViewById(gender.getCheckedRadioButtonId());
- boolean inserted = DB.AddInfo(Username.getText().toString(),DOB.getText().toString(),selected.getText().toString(),Password.getText().toString());
- if(inserted == true) {
- Toast.makeText(ProfileManagement.this, "Data is Inserted", Toast.LENGTH_LONG).show();
- OpenActivity1();
- }
- else {
- Toast.makeText(ProfileManagement.this, "Data isn't Inserted", Toast.LENGTH_LONG).show();
- }
- }
- });
- }
- public void OpenActivity1(){
- Intent intent = new Intent(this,EditProfile.class);
- startActivity(intent);
- }
- }
Add Comment
Please, Sign In to add comment