Guest User

Untitled

a guest
Oct 14th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.89 KB | None | 0 0
  1. package DataBase;
  2. import android.provider.BaseColumns;
  3.  
  4. public class UserProfile{
  5.  
  6. private UserProfile(){
  7.  
  8. }
  9.  
  10. public class User implements BaseColumns {
  11.  
  12. public static final String TABLE = "UserInfo";
  13. public static final String Profile_ID_Col = "ID";
  14. public static final String Prof_Name_Col = "userName";
  15. public static final String DOB_Col = "dateOfBirth";
  16. public static final String gender_Col = "Gender";
  17. public static final String password_col = "Password";
  18.  
  19.  
  20. }
  21. }
  22.  
  23. package DataBase;
  24.  
  25. import android.content.ContentValues;
  26. import android.content.Context;
  27. import android.database.Cursor;
  28. import android.database.sqlite.SQLiteDatabase;
  29. import android.database.sqlite.SQLiteOpenHelper;
  30. import android.widget.EditText;
  31.  
  32. public class DBHelper extends SQLiteOpenHelper {
  33.  
  34. public static final int DATABASE_VERSION = 1;
  35. public static final String DATABASE_NAME = "UserProfile.db";
  36.  
  37. private SQLiteDatabase DB;
  38. private Cursor RESULTS;
  39. private ContentValues VALUES;
  40.  
  41. public DBHelper(Context context) {
  42.  
  43. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  44. }
  45.  
  46. @Override
  47. public void onCreate(SQLiteDatabase db) {
  48.  
  49. db.execSQL("CREATE TABLE IF NOT EXISTS " +UserProfile.User.TABLE +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,userName TEXT,dateOfBirth TEXT,Gender TEXT,Password TEXT)");
  50.  
  51.  
  52. }
  53.  
  54. @Override
  55. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  56.  
  57. db.execSQL("DROP TABLE IF EXISTS "+UserProfile.User.TABLE);
  58. onCreate(db);
  59. }
  60. public boolean AddInfo(String Name,String dob,String gender, String password ){
  61.  
  62. VALUES = new ContentValues();
  63. DB = this.getWritableDatabase();
  64.  
  65. VALUES.put(UserProfile.User.Prof_Name_Col,Name);
  66. VALUES.put(UserProfile.User.DOB_Col,dob);
  67. VALUES.put(UserProfile.User.gender_Col,gender);
  68. VALUES.put(UserProfile.User.password_col,password);
  69.  
  70. long r = DB.insert(UserProfile.User.TABLE,null,VALUES);
  71.  
  72.  
  73. if (r == -1){
  74. return false;
  75.  
  76. }
  77. else {
  78. return true;
  79. }
  80. }
  81. public Boolean UpdateInfo(String id,String Name,String DOB,String Gender,String password ){
  82.  
  83. DB = this.getWritableDatabase();
  84. VALUES = new ContentValues();
  85. VALUES.put(UserProfile.User.Prof_Name_Col,Name);
  86. VALUES.put(UserProfile.User.DOB_Col,DOB);
  87. VALUES.put(UserProfile.User.gender_Col,Gender);
  88. VALUES.put(UserProfile.User.password_col,password);
  89.  
  90. DB.update(UserProfile.User.TABLE,VALUES,"ID = ?",new String[]{id});
  91.  
  92. return true;
  93.  
  94. }
  95. public Cursor readAllInfor(String id){
  96.  
  97. DB = this.getWritableDatabase();
  98. RESULTS = DB.rawQuery("SELECT * from " +UserProfile.User.TABLE + " WHERE TRIM(ID) = ? ", new String[]{id});
  99. return RESULTS;
  100.  
  101.  
  102. }
  103. public Cursor all(){
  104.  
  105. DB = this.getWritableDatabase();
  106. RESULTS = DB.rawQuery("SELECT * FROM "+UserProfile.User.TABLE,null);
  107. return RESULTS;
  108.  
  109. }
  110. public Integer deleteInfo(String id){
  111.  
  112. DB = this.getWritableDatabase();
  113. return DB.delete(UserProfile.User.TABLE,"ID = ?",new String[]{id});
  114.  
  115. }
  116. }
  117.  
  118. import android.database.Cursor;
  119. import android.support.v7.app.AlertDialog;
  120. import android.support.v7.app.AppCompatActivity;
  121. import android.os.Bundle;
  122. import android.view.View;
  123. import android.widget.Button;
  124. import android.widget.EditText;
  125. import android.widget.RadioButton;
  126. import android.widget.RadioGroup;
  127. import android.widget.Toast;
  128.  
  129. import DataBase.DBHelper;
  130. import DataBase.UserProfile;
  131.  
  132. public class EditProfile extends AppCompatActivity {
  133.  
  134. private DBHelper DB;
  135.  
  136. private Button Edit,Viewall;
  137. private Button Delete;
  138. private Button Search;
  139. private EditText Username,DOB,Password,id;
  140. private RadioGroup gender;
  141. private RadioButton selected;
  142.  
  143.  
  144.  
  145. @Override
  146. protected void onCreate(Bundle savedInstanceState) {
  147. super.onCreate(savedInstanceState);
  148. setContentView(R.layout.activity_edit_profile);
  149.  
  150. DB = new DBHelper(this);
  151.  
  152. id = (EditText)findViewById(R.id.ID);
  153. Username = (EditText)findViewById(R.id.Usernametext);
  154. DOB = (EditText)findViewById(R.id.DOBteED);
  155. Password = (EditText)findViewById(R.id.PasswordTeed);
  156. gender = (RadioGroup)findViewById(R.id.radio);
  157. Edit = (Button)findViewById(R.id.editb);
  158. Delete = (Button)findViewById(R.id.deleteb);
  159. Search = (Button)findViewById(R.id.searchb);
  160. Viewall = (Button)findViewById(R.id.viewAll);
  161.  
  162.  
  163. DeleteData();
  164. UpdateData();
  165. search();
  166. viewDATA();
  167.  
  168.  
  169. }
  170.  
  171. public void DeleteData(){
  172.  
  173. Delete.setOnClickListener(
  174. new View.OnClickListener() {
  175. @Override
  176. public void onClick(View v) {
  177.  
  178. Integer delete = DB.deleteInfo(id.getText().toString());
  179. if(delete > 0){
  180.  
  181. Toast.makeText(EditProfile.this,"Data deleted",Toast.LENGTH_LONG).show();
  182.  
  183. }
  184. else{
  185.  
  186. Toast.makeText(EditProfile.this,"Data isn't Deleted",Toast.LENGTH_LONG).show();
  187.  
  188. }
  189.  
  190. }
  191. }
  192.  
  193.  
  194. );
  195.  
  196. }
  197. public void UpdateData() {
  198. Edit.setOnClickListener(
  199. new View.OnClickListener() {
  200. @Override
  201. public void onClick(View v) {
  202.  
  203. selected = (RadioButton)findViewById(gender.getCheckedRadioButtonId());
  204.  
  205. boolean isUpdate = DB.UpdateInfo(id.getText().toString(),Username.getText().toString(),DOB.getText().toString(), selected.getText().toString() ,Password.getText().toString());
  206. if(isUpdate == true)
  207. Toast.makeText(EditProfile.this,"Data is Updated",Toast.LENGTH_LONG).show();
  208. else
  209. Toast.makeText(EditProfile.this,"Data isn't Updated",Toast.LENGTH_LONG).show();
  210. }
  211. }
  212. );
  213. }
  214. public void search(){
  215.  
  216. Search.setOnClickListener(new View.OnClickListener() {
  217. @Override
  218. public void onClick(View v) {
  219. Cursor res = DB.readAllInfor(id.getText().toString());
  220. res.moveToFirst();
  221.  
  222. String name = res.getString(res.getColumnIndex(UserProfile.User.Prof_Name_Col));
  223. String dob = res.getString(res.getColumnIndex(UserProfile.User.DOB_Col));
  224. String gend = res.getString(res.getColumnIndexOrThrow(UserProfile.User.gender_Col));
  225. String pass = res.getColumnName(res.getColumnIndex(UserProfile.User.password_col));
  226.  
  227. if(!res.isClosed()){
  228.  
  229. res.close();
  230.  
  231. }
  232.  
  233. Username.setText((CharSequence)name);
  234. DOB.setText((CharSequence)dob);
  235.  
  236. if(gend == "Male"){
  237.  
  238. RadioButton male = (RadioButton)findViewById(R.id.Male);
  239. male.setChecked(true);
  240.  
  241. }
  242. else if(gend == "Female"){
  243.  
  244. RadioButton female = (RadioButton)findViewById(R.id.Female);
  245. female.setChecked(true);
  246. }
  247.  
  248. Password.setText((CharSequence)pass);
  249.  
  250.  
  251. }
  252. });
  253.  
  254.  
  255. }
  256. public void viewDATA(){
  257. Viewall.setOnClickListener(new View.OnClickListener() {
  258. @Override
  259. public void onClick(View v) {
  260.  
  261. Cursor Vieww = DB.all();
  262. if(Vieww.getCount() == 0){
  263.  
  264. Toast.makeText(EditProfile.this,"Found nothing",Toast.LENGTH_LONG).show();
  265. return;
  266. }
  267. StringBuffer buffer = new StringBuffer();
  268. while (Vieww.moveToNext()){
  269.  
  270. buffer.append("ID : "+Vieww.getString(0)+"n");
  271. buffer.append("Username : "+Vieww.getString(1)+"n");
  272. buffer.append("Date of birth : "+Vieww.getString(2)+"n");
  273. buffer.append("Gender : "+Vieww.getString(3)+"n");
  274. buffer.append("Password : "+Vieww.getString(4)+"nnn");
  275.  
  276. }
  277. showMessage("Data",buffer.toString());
  278.  
  279.  
  280. }
  281. });
  282.  
  283. }
  284. public void showMessage(String title,String Message) {
  285. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  286. builder.setCancelable(true);
  287. builder.setTitle(title);
  288. builder.setMessage(Message);
  289. builder.show();
  290. }
  291.  
  292.  
  293. }
  294.  
  295. import android.content.Intent;
  296. import android.support.v7.app.AppCompatActivity;
  297. import android.os.Bundle;
  298. import android.view.View;
  299. import android.widget.Button;
  300. import android.widget.EditText;
  301. import android.widget.Toast;
  302.  
  303. import DataBase.DBHelper;
  304.  
  305. public class Home extends AppCompatActivity {
  306.  
  307. private EditText Username,Password;
  308. private Button register;
  309. private DBHelper DB1;
  310.  
  311.  
  312. @Override
  313. protected void onCreate(Bundle savedInstanceState) {
  314. super.onCreate(savedInstanceState);
  315. setContentView(R.layout.activity_home);
  316.  
  317. DB1 = new DBHelper(this);
  318.  
  319. Username = (EditText)findViewById(R.id.UsernameHomeTe);
  320. Password = (EditText)findViewById(R.id.PasswordHomeTe);
  321.  
  322. register = (Button)findViewById(R.id.registerBu);
  323. register.setOnClickListener(new View.OnClickListener() {
  324. @Override
  325. public void onClick(View view) {
  326.  
  327. OpenActivity();
  328.  
  329. }
  330. });
  331.  
  332.  
  333.  
  334. }
  335. public void OpenActivity(){
  336.  
  337. Intent intent = new Intent(this,ProfileManagement.class);
  338. startActivity(intent);
  339. }
  340.  
  341.  
  342. }
  343.  
  344. import android.content.Intent;
  345. import android.database.Cursor;
  346. import android.support.v7.app.AlertDialog;
  347. import android.support.v7.app.AppCompatActivity;
  348. import android.os.Bundle;
  349. import android.view.View;
  350. import android.widget.Button;
  351. import android.widget.EditText;
  352. import android.widget.RadioButton;
  353. import android.widget.RadioGroup;
  354. import android.widget.Toast;
  355.  
  356. import DataBase.DBHelper;
  357.  
  358. public class ProfileManagement extends AppCompatActivity {
  359.  
  360. private DBHelper DB;
  361.  
  362. private Button Updateb;
  363. private EditText Username,DOB,Password,id;
  364. private RadioGroup gender;
  365. private RadioButton selected;
  366.  
  367. @Override
  368. protected void onCreate(Bundle savedInstanceState) {
  369. super.onCreate(savedInstanceState);
  370. setContentView(R.layout.activity_profile_management);
  371.  
  372. DB = new DBHelper(this);
  373.  
  374. id = (EditText)findViewById(R.id.ID);
  375. Username = (EditText)findViewById(R.id.UsernameprofEd);
  376. DOB = (EditText)findViewById(R.id.DOBtepro);
  377. gender = (RadioGroup)findViewById(R.id.radioo);
  378. Password = (EditText)findViewById(R.id.PasswordTepro);
  379. Updateb = (Button)findViewById(R.id.Updatebu);
  380.  
  381.  
  382. Updateb.setOnClickListener(new View.OnClickListener() {
  383. @Override
  384. public void onClick(View view) {
  385.  
  386. System.out.println("///////////////////////////////////////////////////////////////////////////////");
  387. selected = (RadioButton)findViewById(gender.getCheckedRadioButtonId());
  388. boolean inserted = DB.AddInfo(Username.getText().toString(),DOB.getText().toString(),selected.getText().toString(),Password.getText().toString());
  389.  
  390.  
  391. if(inserted == true) {
  392. Toast.makeText(ProfileManagement.this, "Data is Inserted", Toast.LENGTH_LONG).show();
  393. OpenActivity1();
  394.  
  395.  
  396.  
  397. }
  398. else {
  399. Toast.makeText(ProfileManagement.this, "Data isn't Inserted", Toast.LENGTH_LONG).show();
  400. }
  401.  
  402.  
  403.  
  404. }
  405. });
  406.  
  407.  
  408.  
  409.  
  410. }
  411.  
  412.  
  413. public void OpenActivity1(){
  414.  
  415. Intent intent = new Intent(this,EditProfile.class);
  416. startActivity(intent);
  417. }
  418.  
  419.  
  420.  
  421.  
  422.  
  423. }
Add Comment
Please, Sign In to add comment