Advertisement
Guest User

Untitled

a guest
May 28th, 2015
4,891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.30 KB | None | 0 0
  1. package com.joelmale.android.contactmanager;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.net.Uri;
  9. import android.os.ParcelFileDescriptor;
  10. import android.support.v7.app.ActionBarActivity;
  11. import android.os.Bundle;
  12. import android.text.Editable;
  13. import android.text.TextWatcher;
  14. import android.view.ContextMenu;
  15. import android.view.Menu;
  16. import android.view.MenuItem;
  17. import android.view.View;
  18. import android.view.ViewGroup;
  19. import android.widget.AdapterView;
  20. import android.widget.ArrayAdapter;
  21. import android.widget.Button;
  22. import android.widget.EditText;
  23. import android.widget.ImageView;
  24. import android.widget.ListView;
  25. import android.widget.TabHost;
  26. import android.widget.TextView;
  27. import android.widget.Toast;
  28.  
  29. import java.io.FileDescriptor;
  30. import java.io.IOException;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33.  
  34. public class MainActivity extends Activity {
  35.  
  36. private static final int EDIT_OPTION = 0, DELETE_OPTION = 1;
  37.  
  38. // Create our edit text variables
  39. EditText nameTxt, phoneTxt, emailTxt, addressTxt;
  40. ImageView contactImage;
  41. // <> automatically uses the first object type
  42. public static List<Contact> Contacts = new ArrayList<>();
  43. ListView contactListView;
  44.  
  45. Uri imageUri = null;
  46.  
  47. DatabaseHandler dbHandler;
  48.  
  49. // Context menu for manipulation
  50. public static int longClickedItemIndex;
  51. ArrayAdapter<Contact> contactAdapter;
  52.  
  53. public static Context mainContext;
  54.  
  55. @Override
  56. protected void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58. setContentView(R.layout.activity_main);
  59.  
  60. mainContext = getApplicationContext();
  61.  
  62. /*
  63. While creating these, make sure to cast the type. Like (EditText), (Button), etc.
  64. */
  65.  
  66. // Assign the edit text variable to the txtContactName ID by using findViewByID
  67. nameTxt = (EditText) findViewById(R.id.txtContactName);
  68.  
  69. // Do the same for the other edit text variables
  70. phoneTxt = (EditText) findViewById(R.id.txtPhoneNumber);
  71. emailTxt = (EditText) findViewById(R.id.txtEmail);
  72. addressTxt = (EditText) findViewById(R.id.txtAddress);
  73.  
  74. // Assign our list view
  75. contactListView = (ListView) findViewById(R.id.listView);
  76.  
  77. // Assign our image view
  78. contactImage = (ImageView) findViewById(R.id.ivContactImage);
  79.  
  80. // Assign our dbhandler
  81. dbHandler = new DatabaseHandler(getApplicationContext());
  82.  
  83. // register the context menu on our list view
  84. registerForContextMenu(contactListView);
  85.  
  86. contactListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  87. @Override
  88. public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
  89. longClickedItemIndex = position;
  90. return false;
  91. }
  92. });
  93.  
  94. setUpTabs();
  95.  
  96. // Create the button
  97. final Button addBtn = (Button) findViewById(R.id.btnAdd);
  98. addBtn.setOnClickListener(new View.OnClickListener() {
  99. @Override
  100. public void onClick(View v) {
  101. // Create a a toast window to display a message
  102. if (imageUri == null) {
  103. imageUri = resIdToUri(getApplicationContext(), R.drawable.nouserlogo);
  104. }
  105. Contact newContact = new Contact(dbHandler.getContactsCount(), String.valueOf(nameTxt.getText()), String.valueOf(phoneTxt.getText()), String.valueOf(emailTxt.getText()), String.valueOf(addressTxt.getText()), imageUri);
  106. if (!contactExists(newContact)) {
  107. dbHandler.createContact(newContact);
  108. Contacts.add(newContact);
  109. // Let our adapater know data was changed
  110. contactAdapter.notifyDataSetChanged();
  111. Toast.makeText(getApplicationContext(), String.valueOf(nameTxt.getText()) + " was successfully added.", Toast.LENGTH_SHORT).show();
  112. } else {
  113. Toast.makeText(getApplicationContext(), String.valueOf(nameTxt.getText()) + " already exists. Please use a different name.", Toast.LENGTH_SHORT).show();
  114. }
  115. clearData();
  116. return;
  117. }
  118. });
  119.  
  120. nameTxt.addTextChangedListener(new TextWatcher() {
  121. @Override
  122. public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
  123.  
  124. @Override
  125. public void onTextChanged(CharSequence s, int start, int before, int count) {
  126. // Set the button to enabled if the nameTxt to string value
  127. // Trim all the whitespace with trim()
  128. // isEmpty to see if it's "" or not
  129. addBtn.setEnabled(!String.valueOf(nameTxt.getText()).trim().isEmpty());
  130. }
  131.  
  132. @Override
  133. public void afterTextChanged(Editable s) {}
  134. });
  135.  
  136. // New click listener for the image
  137. contactImage.setOnClickListener(new View.OnClickListener() {
  138. @Override
  139. public void onClick(View view) {
  140. // Open selection for gallery
  141. // Create a new intent
  142. Intent newImage = new Intent();
  143. // Set type to image
  144. newImage.setType("image/*");
  145. // Set the intent action
  146. newImage.setAction(Intent.ACTION_GET_CONTENT);
  147. // Start the activity for result (meaning start intent expecting a result)
  148. startActivityForResult(Intent.createChooser(newImage, "Select Contact Image"), 1);
  149. }
  150. });
  151.  
  152. if (dbHandler.getContactsCount() != 0)
  153. Contacts.addAll(dbHandler.getAllContacts());
  154.  
  155. populateList();
  156. }
  157.  
  158. // Our context menu
  159. public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
  160. super.onCreateContextMenu(menu, v, menuInfo);
  161.  
  162. // Our context
  163. // Set title and icons
  164. menu.setHeaderIcon(R.drawable.edit_contact_icon);
  165. menu.setHeaderTitle("Contact Options");
  166.  
  167. // Add our menus
  168. // First: group id
  169. // Second: item id
  170. // Third: order
  171. // Fourth: name of it
  172. menu.add(Menu.NONE, EDIT_OPTION, menu.NONE, "Edit Contact");
  173. menu.add(Menu.NONE, DELETE_OPTION, menu.NONE, "Delete Contact");
  174. }
  175.  
  176. public boolean onContextItemSelected(MenuItem item) {
  177. switch (item.getItemId()) {
  178. case EDIT_OPTION:
  179. //TODO Implement editing a contact
  180. Intent editContactIntent = new Intent(getApplicationContext(), EditContact.class);
  181. startActivityForResult(editContactIntent, 2);
  182. break;
  183. case DELETE_OPTION:
  184. dbHandler.deleteContact(Contacts.get(longClickedItemIndex));
  185. Contacts.remove(longClickedItemIndex);
  186. contactAdapter.notifyDataSetChanged();
  187. break;
  188. }
  189.  
  190. return super.onContextItemSelected(item);
  191. }
  192.  
  193. private boolean contactExists(Contact c) {
  194. String name = c.getName();
  195. int contactCount = Contacts.size();
  196.  
  197. for (int i = 0; i < contactCount; i++) {
  198. if (name.compareToIgnoreCase(Contacts.get(i).getName()) == 0)
  199. return true;
  200. }
  201. return false;
  202. }
  203.  
  204. @Override
  205. public boolean onCreateOptionsMenu(Menu menu) {
  206. // Inflate the menu; this adds items to the action bar if it is present.
  207. getMenuInflater().inflate(R.menu.menu_main, menu);
  208. return true;
  209. }
  210.  
  211. @Override
  212. public boolean onOptionsItemSelected(MenuItem item) {
  213. // Handle action bar item clicks here. The action bar will
  214. // automatically handle clicks on the Home/Up button, so long
  215. // as you specify a parent activity in AndroidManifest.xml.
  216. int id = item.getItemId();
  217.  
  218. //noinspection SimplifiableIfStatement
  219. if (id == R.id.action_settings) {
  220. return true;
  221. }
  222.  
  223. return super.onOptionsItemSelected(item);
  224. }
  225.  
  226. private void clearData() {
  227. contactImage.setImageURI(resIdToUri(getApplicationContext(), R.drawable.nouserlogo));
  228. nameTxt.setText("");
  229. phoneTxt.setText("");
  230. emailTxt.setText("");
  231. addressTxt.setText("");
  232. imageUri = null;
  233. }
  234.  
  235. private void setUpTabs() {
  236. // Create our TabHost
  237. TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
  238.  
  239. // Call the tab host set up function
  240. tabHost.setup();
  241.  
  242. // Create a tabSpec for our first tab and assign the content
  243. TabHost.TabSpec tabSpec = tabHost.newTabSpec("creator");
  244. tabSpec.setContent(R.id.tabCreater);
  245.  
  246. // Set the indicator (text label) for our first tab
  247. tabSpec.setIndicator("Creator");
  248. tabHost.addTab(tabSpec);
  249.  
  250. // Second spec
  251. tabSpec = tabHost.newTabSpec("list");
  252. tabSpec.setContent(R.id.tabContactList);
  253.  
  254. // Set the indicator (text label) for our first tab
  255. tabSpec.setIndicator("List");
  256. tabHost.addTab(tabSpec);
  257. }
  258.  
  259. // Function to capture our start activity for result (dont need override)
  260. public void onActivityResult(int reqCode, int resCode, Intent data) {
  261. // Check if the intent is ok by checking if the result code is RESULT_OK
  262. if (resCode == RESULT_OK) {
  263. // If the request code is 1
  264. if (reqCode == 1) {
  265. // Set the variable
  266. imageUri = data.getData();
  267. // Set the contact image with the data's url
  268. contactImage.setImageURI(imageUri);
  269. } else if (reqCode == 2) {
  270. // stuff for edit contact
  271. contactAdapter.notifyDataSetChanged();
  272. populateList();
  273. }
  274. }
  275. }
  276.  
  277. private void populateList() {
  278. contactAdapter = new ContactListAdapter();
  279. contactListView.setAdapter(contactAdapter);
  280. contactAdapter.notifyDataSetChanged();
  281. }
  282.  
  283. /*
  284. I DONT UNDERSTAND HOW THIS WORKS, SO GO BACK AND FIND OUT. (ep 3 or 4 probably)
  285. */
  286. private class ContactListAdapter extends ArrayAdapter<Contact> {
  287. public ContactListAdapter() {
  288. // comment this
  289. super(MainActivity.this, R.layout.listview_item, Contacts);
  290. }
  291.  
  292. @Override
  293. public View getView(int pos, View view, ViewGroup parent) {
  294. // Check if view is inflated by layout
  295. // If the view is null (aka not inflated)
  296. if (view == null)
  297. // Then inflate it using getLayout, pass in the listview.xml layout, the parent layout
  298. // which will be activity_main, and false to not attach to root
  299. view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);
  300.  
  301. // Create a currentContact object in for the contacts list at index pos
  302. Contact currentContact = Contacts.get(pos);
  303.  
  304. // Set image view
  305. ImageView ivContactImage = (ImageView) view.findViewById(R.id.ivContactImage);
  306. ivContactImage.setImageURI(currentContact.getUri());
  307.  
  308. // Set our variables equal to current contacts information
  309. TextView name = (TextView) view.findViewById(R.id.txtContactName);
  310. name.setText(currentContact.getName());
  311.  
  312. TextView phone = (TextView) view.findViewById(R.id.txtPhoneNumber);
  313. phone.setText(currentContact.getPhone());
  314.  
  315. TextView email = (TextView) view.findViewById(R.id.txtEmailAddress);
  316. email.setText(currentContact.getEmail());
  317.  
  318. TextView address = (TextView) view.findViewById(R.id.txtAddress);
  319. address.setText(currentContact.getAddress());
  320.  
  321. // Return our view
  322. return view;
  323. }
  324. }
  325.  
  326. public static Uri resIdToUri(Context context, int resId) {
  327. return Uri.parse("android.resource://" + context.getPackageName()
  328. + "/" + resId);
  329.  
  330. }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement