Guest User

Untitled

a guest
Oct 21st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. package com.blogspot.dbh4ck.sqlite_demo_db;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.util.Log;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.ArrayAdapter;
  15. import android.widget.Button;
  16. import android.widget.ListView;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19.  
  20. import java.util.ArrayList;
  21.  
  22. public class MainActivity extends Activity {
  23. private Boolean exit = false;
  24. Button add_btn;
  25. ListView Contact_listview;
  26. ArrayList<Contact> contact_data = new ArrayList<Contact>();
  27. Contact_Adapter cAdapter;
  28. DatabaseHandler db;
  29. String Toast_msg;
  30.  
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main);
  35. try {
  36. Contact_listview = (ListView) findViewById(R.id.list);
  37. Contact_listview.setItemsCanFocus(false);
  38. add_btn = (Button) findViewById(R.id.add_btn);
  39.  
  40. Set_Referash_Data();
  41.  
  42. } catch (Exception e) {
  43. // TODO: handle exception
  44. Log.e("some error", "" + e);
  45. }
  46.  
  47. add_btn.setOnClickListener(new View.OnClickListener() {
  48.  
  49. @Override
  50. public void onClick(View v) {
  51. // TODO Auto-generated method stub
  52. Intent add_user = new Intent(MainActivity.this, Add_Update_User.class);
  53. add_user.putExtra("called", "add");
  54. add_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  55. startActivity(add_user);
  56. finish();
  57. }
  58. });
  59. }
  60.  
  61.  
  62. public void Set_Referash_Data() {
  63. contact_data.clear();
  64. db = new DatabaseHandler(this);
  65. ArrayList<Contact> contact_array_from_db = db.Get_Contacts();
  66.  
  67. for (int i = 0; i < contact_array_from_db.size(); i++) {
  68.  
  69. int tidno = contact_array_from_db.get(i).getID();
  70. String name = contact_array_from_db.get(i).getName();
  71. String email = contact_array_from_db.get(i).getEmail();
  72. // byte[] image = contact_array_from_db.get(i).getImage();
  73.  
  74. Contact cnt = new Contact();
  75. cnt.setID(tidno);
  76. cnt.setName(name);
  77. cnt.setEmail(email);
  78. // cnt.setImage(image);
  79.  
  80. contact_data.add(cnt);
  81. }
  82. db.close();
  83. cAdapter = new Contact_Adapter(MainActivity.this, R.layout.listview_row, contact_data);
  84. Contact_listview.setAdapter(cAdapter);
  85. cAdapter.notifyDataSetChanged();
  86. }
  87.  
  88. public void Show_Toast(String msg) {
  89. Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
  90. }
  91.  
  92. @Override
  93. public void onResume() {
  94. // TODO Auto-generated method stub
  95. super.onResume();
  96. Set_Referash_Data();
  97.  
  98. }
  99.  
  100.  
  101. @Override
  102. public void onBackPressed()
  103. {
  104. // super.onBackPressed();
  105. if(exit){
  106. finish();
  107. }
  108. else{
  109. Toast.makeText(this, "Press Back Again To Exit" , Toast.LENGTH_SHORT).show();
  110. exit = true;
  111.  
  112. new Handler().postDelayed(new Runnable() {
  113. @Override
  114. public void run() {
  115. Intent dbdito = new Intent(Intent.ACTION_MAIN);
  116. dbdito.addCategory(Intent.CATEGORY_HOME);
  117. dbdito.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  118. startActivity(dbdito);
  119. }
  120. }, 1000);
  121. }
  122. }
  123.  
  124.  
  125. public class Contact_Adapter extends ArrayAdapter<Contact> {
  126.  
  127. Context context;
  128. int layoutResourceId;
  129. Contact user;
  130. ArrayList<Contact> data = new ArrayList<Contact>();
  131.  
  132. public Contact_Adapter(Context context, int layoutResourceId, ArrayList<Contact> data) {
  133. super(context, layoutResourceId, data);
  134. this.layoutResourceId = layoutResourceId;
  135. this.context = context;
  136. this.data = data;
  137. notifyDataSetChanged();
  138. }
  139.  
  140. @Override
  141. public View getView(int position, View convertView, ViewGroup parent) {
  142. View row = convertView;
  143. UserHolder holder = null;
  144.  
  145. if (row == null) {
  146. LayoutInflater inflater = ((Activity)context).getLayoutInflater();
  147.  
  148. row = inflater.inflate(layoutResourceId, parent, false);
  149. holder = new UserHolder();
  150. holder.name = (TextView) row.findViewById(R.id.user_name_txt);
  151. holder.email = (TextView) row.findViewById(R.id.user_email_txt);
  152.  
  153. holder.edit = (Button) row.findViewById(R.id.btn_update);
  154. holder.delete = (Button) row.findViewById(R.id.btn_delete);
  155.  
  156. row.setTag(holder);
  157. } else {
  158. holder = (UserHolder) row.getTag();
  159. }
  160. user = data.get(position);
  161. holder.edit.setTag(user.getID());
  162. holder.delete.setTag(user.getID());
  163. holder.name.setText(user.getName());
  164. holder.email.setText(user.getEmail());
  165.  
  166. holder.edit.setOnClickListener(new View.OnClickListener() {
  167.  
  168. @Override
  169. public void onClick(View v) {
  170. // TODO Auto-generated method stub
  171. Log.i("Edit Button Clicked", "**********");
  172.  
  173. Intent update_user = new Intent(context, Add_Update_User.class);
  174. update_user.putExtra("called", "update");
  175. update_user.putExtra("USER_ID", v.getTag().toString());
  176. context.startActivity(update_user);
  177.  
  178. }
  179. });
  180. holder.delete.setOnClickListener(new View.OnClickListener() {
  181.  
  182. @Override
  183. public void onClick(final View v) {
  184. // TODO Auto-generated method stub
  185.  
  186. // show a message while loader is loading
  187.  
  188. AlertDialog.Builder adb = new AlertDialog.Builder(context);
  189. adb.setTitle("Delete?");
  190. adb.setMessage("Are you sure you want to delete");
  191. final int user_id = Integer.parseInt(v.getTag().toString());
  192. adb.setNegativeButton("Cancel", null);
  193. adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
  194. @Override
  195. public void onClick(DialogInterface dialog, int which) {
  196. // MyDataObject.remove(positionToRemove);
  197. DatabaseHandler dBHandler = new DatabaseHandler(context.getApplicationContext());
  198. dBHandler.Delete_Contact(user_id);
  199.  
  200. Toast.makeText(getApplicationContext(), "Deleted!", Toast.LENGTH_LONG).show();
  201. // MainActivity
  202. MainActivity.this.onResume();
  203. }
  204.  
  205. });
  206. adb.show();
  207. }
  208.  
  209. });
  210. return row;
  211.  
  212. }
  213.  
  214. class UserHolder {
  215. TextView name;
  216. TextView email;
  217. Button edit;
  218. Button delete;
  219. }
  220.  
  221. }
  222.  
  223.  
  224. }
Add Comment
Please, Sign In to add comment