Guest User

Untitled

a guest
Oct 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. package com.blogspot.dbh4ck.sqlite_demo_db;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.text.Editable;
  7. import android.text.TextWatcher;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.LinearLayout;
  12. import android.widget.Toast;
  13.  
  14. public class Add_Update_User extends Activity {
  15. // Bitmap theImage;
  16. // ImageView imageDetail;
  17. EditText add_name, add_email;
  18. Button add_save_btn, add_view_all, update_btn, update_view_all;
  19. LinearLayout add_view, update_view;
  20. String valid_email = null, valid_name = null, Toast_msg = null, valid_user_id = "";
  21. int USER_ID, imageId;
  22.  
  23. DatabaseHandler dbHandler = new DatabaseHandler(this);
  24.  
  25.  
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. // TODO Auto-generated method stub
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.add_update_screen);
  31.  
  32. // set screen
  33. Set_Add_Update_Screen();
  34.  
  35. // set visibility of view as per activity calls
  36. String called_from = getIntent().getStringExtra("called");
  37.  
  38. if (called_from.equalsIgnoreCase("add")) {
  39. add_view.setVisibility(View.VISIBLE);
  40. update_view.setVisibility(View.GONE);
  41. } else {
  42.  
  43. update_view.setVisibility(View.VISIBLE);
  44. add_view.setVisibility(View.GONE);
  45. USER_ID = Integer.parseInt(getIntent().getStringExtra("USER_ID"));
  46.  
  47. Contact c = dbHandler.Get_Contact(USER_ID);
  48.  
  49. add_name.setText(c.getName());
  50. add_email.setText(c.getEmail());
  51. // dbHandler.close();
  52. }
  53.  
  54. add_email.addTextChangedListener(new TextWatcher() {
  55.  
  56. @Override
  57. public void onTextChanged(CharSequence s, int start, int before,
  58. int count) {
  59. // TODO Auto-generated method stub
  60.  
  61. }
  62.  
  63. @Override
  64. public void beforeTextChanged(CharSequence s, int start, int count,
  65. int after) {
  66. // TODO Auto-generated method stub
  67.  
  68. }
  69.  
  70. @Override
  71. public void afterTextChanged(Editable s) {
  72. // TODO Auto-generated method stub
  73. Is_Valid_Email(add_email);
  74. }
  75. });
  76.  
  77. add_name.addTextChangedListener(new TextWatcher() {
  78.  
  79. @Override
  80. public void onTextChanged(CharSequence s, int start, int before,
  81. int count) {
  82. // TODO Auto-generated method stub
  83.  
  84. }
  85.  
  86. @Override
  87. public void beforeTextChanged(CharSequence s, int start, int count,
  88. int after) {
  89. // TODO Auto-generated method stub
  90.  
  91. }
  92.  
  93. @Override
  94. public void afterTextChanged(Editable s) {
  95. // TODO Auto-generated method stub
  96. Is_Valid_Person_Name(add_name);
  97. }
  98. });
  99.  
  100. // imageDetail = (ImageView) findViewById(R.id.imageView);
  101. // Intent intnt = getIntent();
  102. // theImage = (Bitmap) intnt.getParcelableExtra("imagename");
  103. // imageId = intnt.getIntExtra("imageid", 20);
  104. // Log.d("Image ID:****", String.valueOf(imageId));
  105. // imageDetail.setImageBitmap(theImage);
  106.  
  107.  
  108. add_save_btn.setOnClickListener(new View.OnClickListener() {
  109.  
  110. @Override
  111. public void onClick(View v) {
  112. // TODO Auto-generated method stub
  113. // check the value state is null or not
  114. if (valid_name != null && valid_email != null && valid_name.length() != 0 && valid_email.length() != 0) {
  115.  
  116. dbHandler.Add_Contact(new Contact(valid_name, valid_email));
  117. Toast_msg = "Data Inserted Successfully";
  118. Show_Toast(Toast_msg);
  119. Reset_Text();
  120.  
  121. }
  122.  
  123. }
  124. });
  125.  
  126. update_btn.setOnClickListener(new View.OnClickListener() {
  127.  
  128. @Override
  129. public void onClick(View v) {
  130. // TODO Auto-generated method stub
  131.  
  132. valid_name = add_name.getText().toString();
  133. valid_email = add_email.getText().toString();
  134.  
  135. // check the value state is null or not
  136. if (valid_name != null && valid_email != null && valid_name.length() != 0 && valid_email.length() != 0) {
  137.  
  138. dbHandler.Update_Contact(new Contact(USER_ID, valid_name, valid_email));
  139. dbHandler.close();
  140. Toast_msg = "Data Updated Successfully";
  141. Show_Toast(Toast_msg);
  142. Reset_Text();
  143. } else {
  144. Toast_msg = "Sorry some fields are Missing.\nPlease fill up them.";
  145. Show_Toast(Toast_msg);
  146. }
  147.  
  148. }
  149. });
  150.  
  151. update_view_all.setOnClickListener(new View.OnClickListener() {
  152.  
  153. @Override
  154. public void onClick(View v) {
  155. // TODO Auto-generated method stub
  156. // After Making necesssary updates we Return to MainActivity
  157. Intent view_user = new Intent(Add_Update_User.this, MainActivity.class);
  158. view_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  159. startActivity(view_user);
  160. finish();
  161. }
  162. });
  163.  
  164. add_view_all.setOnClickListener(new View.OnClickListener() {
  165.  
  166. @Override
  167. public void onClick(View v) {
  168. // TODO Auto-generated method stub
  169. Intent view_user = new Intent(Add_Update_User.this, MainActivity.class);
  170. view_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  171. startActivity(view_user);
  172. finish();
  173. }
  174. });
  175.  
  176. }
  177.  
  178. public void Set_Add_Update_Screen() {
  179.  
  180. add_name = (EditText) findViewById(R.id.add_name);
  181. add_email = (EditText) findViewById(R.id.add_email);
  182.  
  183. add_save_btn = (Button) findViewById(R.id.add_save_btn);
  184. update_btn = (Button) findViewById(R.id.update_btn);
  185. add_view_all = (Button) findViewById(R.id.add_view_all);
  186. update_view_all = (Button) findViewById(R.id.update_view_all);
  187.  
  188. add_view = (LinearLayout) findViewById(R.id.add_view);
  189. update_view = (LinearLayout) findViewById(R.id.update_view);
  190.  
  191. add_view.setVisibility(View.GONE);
  192. update_view.setVisibility(View.GONE);
  193.  
  194. }
  195.  
  196.  
  197. public void Is_Valid_Email(EditText edt) {
  198. if (edt.getText().toString() == null) {
  199. edt.setError("Invalid Email Address");
  200. valid_email = null;
  201. } else if (isEmailValid(edt.getText().toString()) == false) {
  202. edt.setError("Invalid Email Address");
  203. valid_email = null;
  204. } else {
  205. valid_email = edt.getText().toString();
  206. }
  207. }
  208.  
  209. boolean isEmailValid(CharSequence email) {
  210. return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
  211. } // end of email matcher
  212.  
  213. public void Is_Valid_Person_Name(EditText edt) throws NumberFormatException {
  214. if (edt.getText().toString().length() <= 0) {
  215. edt.setError("Accept Alphabets Only.");
  216. valid_name = null;
  217. } else if (!edt.getText().toString().matches("[a-zA-Z ]+")) {
  218. edt.setError("Only Alphabets Allowed!");
  219. valid_name = null;
  220. } else {
  221. valid_name = edt.getText().toString();
  222. }
  223.  
  224. }
  225.  
  226. public void Show_Toast(String msg) {
  227. Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
  228. }
  229.  
  230. public void Reset_Text() {
  231. add_name.getText().clear();
  232. add_email.getText().clear();
  233. }
  234.  
  235. }
Add Comment
Please, Sign In to add comment