· 7 years ago · Oct 14, 2018, 08:18 PM
1package com.bla.androidsqlitebasics.activity;
2
3import android.content.Intent;
4import android.os.Bundle;
5import android.support.v7.app.AppCompatActivity;
6import android.view.View;
7import android.widget.Button;
8import android.widget.EditText;
9import android.widget.RadioGroup;
10import android.widget.Toast;
11
12import com.bla.androidsqlitebasics.database.DBHelper;
13import com.bla.androidsqlitebasics.model.User;
14import com.live.sinhalacoder.androidsqlitebasics.R;
15
16import java.util.List;
17
18public class EditProfileActivity extends AppCompatActivity {
19
20 EditText usernameEt, passwordEt, dobEt;
21 RadioGroup genderRadio;
22 Button editBt, searchBt, deleteBt;
23
24 //to get access to database table
25 DBHelper mHelper;
26
27 long userId;
28
29 @Override
30 protected void onCreate(Bundle savedInstanceState) {
31 super.onCreate(savedInstanceState);
32 setContentView(R.layout.activity_edit_profile);
33
34 usernameEt = findViewById(R.id.idUsernameEt);
35 passwordEt = findViewById(R.id.idPasswordEt);
36 dobEt = findViewById(R.id.idBirthdayEt);
37
38 genderRadio = findViewById(R.id.radioGroup);
39
40 editBt = findViewById(R.id.idEditBt);
41 searchBt = findViewById(R.id.idSearchBt);
42 deleteBt = findViewById(R.id.idDeleteBt);
43
44 //initialize db helper when app create
45 mHelper = new DBHelper(this);
46
47 //TODO: get userId that is coming from the home activity to search using user Id(not sure this way or using search username)
48 //If press update or delete without searching user id coming from the home activity will be deleted
49 Intent intent = getIntent();
50 if (intent != null) {
51 userId = intent.getLongExtra("userId", -1);
52 }
53
54 //if user clicked edit button
55 editBt.setOnClickListener(new View.OnClickListener() {
56 @Override
57 public void onClick(View v) {
58
59 String uname = usernameEt.getText().toString();
60 String pass = passwordEt.getText().toString();
61 String dob = dobEt.getText().toString();
62 String gender = "";
63 if (genderRadio.getCheckedRadioButtonId() == R.id.idMaleRadio) {
64 gender = "Male";
65 } else if (genderRadio.getCheckedRadioButtonId() == R.id.idFemaleRadio) {
66 gender = "Female";
67 }
68
69 //edit logged in user
70 if (mHelper.updateInfo(userId, uname, pass, dob, gender)) {
71 Toast.makeText(EditProfileActivity.this, "Updated!", Toast.LENGTH_SHORT).show();
72 } else {
73 Toast.makeText(EditProfileActivity.this, "Cannot update!", Toast.LENGTH_SHORT).show();
74 }
75 }
76 });
77
78 //if user clicked search button
79 searchBt.setOnClickListener(new View.OnClickListener() {
80 @Override
81 public void onClick(View v) {
82 //get typed username to search
83 String username = usernameEt.getText().toString();
84
85 List<User> users = mHelper.readAllInfor();
86
87 //we dont have loop through to find matching username if we send username as a parameter to the dbhelper and check for that user inside readAllInfo
88 for (User u : users) {
89 //if typed username equals with table value
90 if (username.equals(u.getUsername())) {
91 //get the user id to update and delete
92 userId = u.getId();
93 //if there is any matching username with db user table get those values and place into textfields
94 passwordEt.setText(u.getPassword());
95 dobEt.setText(u.getDob());
96
97 if (u.getGender() != null) {
98 if (u.getGender().equals("Male")) {
99 genderRadio.check(R.id.idMaleRadio);
100 } else if (u.getGender().equals("Female"))
101 genderRadio.check(R.id.idFemaleRadio);
102 }
103 }
104 }
105
106 //dumb trick to display if user not exists
107 if (passwordEt.getText().toString().equals("")) {
108 //if searched user not exists
109 Toast.makeText(EditProfileActivity.this, "No user found!", Toast.LENGTH_SHORT).show();
110 }
111 }
112 });
113
114 //if user clicked delete button
115 deleteBt.setOnClickListener(new View.OnClickListener() {
116 @Override
117 public void onClick(View v) {
118 //delete user from table and if deleted count is greater than 0 display delete message
119 if (mHelper.deleteInfo(userId)) {
120 Toast.makeText(EditProfileActivity.this, "Deleted!", Toast.LENGTH_SHORT).show();
121 } else {
122 Toast.makeText(EditProfileActivity.this, "User not in the table!", Toast.LENGTH_SHORT).show();
123 }
124
125 //clear out editText after deleted
126 usernameEt.setText("");
127 passwordEt.setText("");
128 dobEt.setText("");
129 genderRadio.clearCheck();
130 }
131 });
132 }
133
134 @Override
135 protected void onDestroy() {
136 super.onDestroy();
137 mHelper.close();
138 }
139}