Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joelmale.android.contactmanager;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.ParcelFileDescriptor;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.view.ContextMenu;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TabHost;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.FileDescriptor;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class MainActivity extends Activity {
- private static final int EDIT_OPTION = 0, DELETE_OPTION = 1;
- // Create our edit text variables
- EditText nameTxt, phoneTxt, emailTxt, addressTxt;
- ImageView contactImage;
- // <> automatically uses the first object type
- public static List<Contact> Contacts = new ArrayList<>();
- ListView contactListView;
- Uri imageUri = null;
- DatabaseHandler dbHandler;
- // Context menu for manipulation
- public static int longClickedItemIndex;
- ArrayAdapter<Contact> contactAdapter;
- public static Context mainContext;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mainContext = getApplicationContext();
- /*
- While creating these, make sure to cast the type. Like (EditText), (Button), etc.
- */
- // Assign the edit text variable to the txtContactName ID by using findViewByID
- nameTxt = (EditText) findViewById(R.id.txtContactName);
- // Do the same for the other edit text variables
- phoneTxt = (EditText) findViewById(R.id.txtPhoneNumber);
- emailTxt = (EditText) findViewById(R.id.txtEmail);
- addressTxt = (EditText) findViewById(R.id.txtAddress);
- // Assign our list view
- contactListView = (ListView) findViewById(R.id.listView);
- // Assign our image view
- contactImage = (ImageView) findViewById(R.id.ivContactImage);
- // Assign our dbhandler
- dbHandler = new DatabaseHandler(getApplicationContext());
- // register the context menu on our list view
- registerForContextMenu(contactListView);
- contactListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
- @Override
- public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
- longClickedItemIndex = position;
- return false;
- }
- });
- setUpTabs();
- // Create the button
- final Button addBtn = (Button) findViewById(R.id.btnAdd);
- addBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // Create a a toast window to display a message
- if (imageUri == null) {
- imageUri = resIdToUri(getApplicationContext(), R.drawable.nouserlogo);
- }
- Contact newContact = new Contact(dbHandler.getContactsCount(), String.valueOf(nameTxt.getText()), String.valueOf(phoneTxt.getText()), String.valueOf(emailTxt.getText()), String.valueOf(addressTxt.getText()), imageUri);
- if (!contactExists(newContact)) {
- dbHandler.createContact(newContact);
- Contacts.add(newContact);
- // Let our adapater know data was changed
- contactAdapter.notifyDataSetChanged();
- Toast.makeText(getApplicationContext(), String.valueOf(nameTxt.getText()) + " was successfully added.", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(getApplicationContext(), String.valueOf(nameTxt.getText()) + " already exists. Please use a different name.", Toast.LENGTH_SHORT).show();
- }
- clearData();
- return;
- }
- });
- nameTxt.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- // Set the button to enabled if the nameTxt to string value
- // Trim all the whitespace with trim()
- // isEmpty to see if it's "" or not
- addBtn.setEnabled(!String.valueOf(nameTxt.getText()).trim().isEmpty());
- }
- @Override
- public void afterTextChanged(Editable s) {}
- });
- // New click listener for the image
- contactImage.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- // Open selection for gallery
- // Create a new intent
- Intent newImage = new Intent();
- // Set type to image
- newImage.setType("image/*");
- // Set the intent action
- newImage.setAction(Intent.ACTION_GET_CONTENT);
- // Start the activity for result (meaning start intent expecting a result)
- startActivityForResult(Intent.createChooser(newImage, "Select Contact Image"), 1);
- }
- });
- if (dbHandler.getContactsCount() != 0)
- Contacts.addAll(dbHandler.getAllContacts());
- populateList();
- }
- // Our context menu
- public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
- super.onCreateContextMenu(menu, v, menuInfo);
- // Our context
- // Set title and icons
- menu.setHeaderIcon(R.drawable.edit_contact_icon);
- menu.setHeaderTitle("Contact Options");
- // Add our menus
- // First: group id
- // Second: item id
- // Third: order
- // Fourth: name of it
- menu.add(Menu.NONE, EDIT_OPTION, menu.NONE, "Edit Contact");
- menu.add(Menu.NONE, DELETE_OPTION, menu.NONE, "Delete Contact");
- }
- public boolean onContextItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case EDIT_OPTION:
- //TODO Implement editing a contact
- Intent editContactIntent = new Intent(getApplicationContext(), EditContact.class);
- startActivityForResult(editContactIntent, 2);
- break;
- case DELETE_OPTION:
- dbHandler.deleteContact(Contacts.get(longClickedItemIndex));
- Contacts.remove(longClickedItemIndex);
- contactAdapter.notifyDataSetChanged();
- break;
- }
- return super.onContextItemSelected(item);
- }
- private boolean contactExists(Contact c) {
- String name = c.getName();
- int contactCount = Contacts.size();
- for (int i = 0; i < contactCount; i++) {
- if (name.compareToIgnoreCase(Contacts.get(i).getName()) == 0)
- return true;
- }
- return false;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- private void clearData() {
- contactImage.setImageURI(resIdToUri(getApplicationContext(), R.drawable.nouserlogo));
- nameTxt.setText("");
- phoneTxt.setText("");
- emailTxt.setText("");
- addressTxt.setText("");
- imageUri = null;
- }
- private void setUpTabs() {
- // Create our TabHost
- TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
- // Call the tab host set up function
- tabHost.setup();
- // Create a tabSpec for our first tab and assign the content
- TabHost.TabSpec tabSpec = tabHost.newTabSpec("creator");
- tabSpec.setContent(R.id.tabCreater);
- // Set the indicator (text label) for our first tab
- tabSpec.setIndicator("Creator");
- tabHost.addTab(tabSpec);
- // Second spec
- tabSpec = tabHost.newTabSpec("list");
- tabSpec.setContent(R.id.tabContactList);
- // Set the indicator (text label) for our first tab
- tabSpec.setIndicator("List");
- tabHost.addTab(tabSpec);
- }
- // Function to capture our start activity for result (dont need override)
- public void onActivityResult(int reqCode, int resCode, Intent data) {
- // Check if the intent is ok by checking if the result code is RESULT_OK
- if (resCode == RESULT_OK) {
- // If the request code is 1
- if (reqCode == 1) {
- // Set the variable
- imageUri = data.getData();
- // Set the contact image with the data's url
- contactImage.setImageURI(imageUri);
- } else if (reqCode == 2) {
- // stuff for edit contact
- contactAdapter.notifyDataSetChanged();
- populateList();
- }
- }
- }
- private void populateList() {
- contactAdapter = new ContactListAdapter();
- contactListView.setAdapter(contactAdapter);
- contactAdapter.notifyDataSetChanged();
- }
- /*
- I DONT UNDERSTAND HOW THIS WORKS, SO GO BACK AND FIND OUT. (ep 3 or 4 probably)
- */
- private class ContactListAdapter extends ArrayAdapter<Contact> {
- public ContactListAdapter() {
- // comment this
- super(MainActivity.this, R.layout.listview_item, Contacts);
- }
- @Override
- public View getView(int pos, View view, ViewGroup parent) {
- // Check if view is inflated by layout
- // If the view is null (aka not inflated)
- if (view == null)
- // Then inflate it using getLayout, pass in the listview.xml layout, the parent layout
- // which will be activity_main, and false to not attach to root
- view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);
- // Create a currentContact object in for the contacts list at index pos
- Contact currentContact = Contacts.get(pos);
- // Set image view
- ImageView ivContactImage = (ImageView) view.findViewById(R.id.ivContactImage);
- ivContactImage.setImageURI(currentContact.getUri());
- // Set our variables equal to current contacts information
- TextView name = (TextView) view.findViewById(R.id.txtContactName);
- name.setText(currentContact.getName());
- TextView phone = (TextView) view.findViewById(R.id.txtPhoneNumber);
- phone.setText(currentContact.getPhone());
- TextView email = (TextView) view.findViewById(R.id.txtEmailAddress);
- email.setText(currentContact.getEmail());
- TextView address = (TextView) view.findViewById(R.id.txtAddress);
- address.setText(currentContact.getAddress());
- // Return our view
- return view;
- }
- }
- public static Uri resIdToUri(Context context, int resId) {
- return Uri.parse("android.resource://" + context.getPackageName()
- + "/" + resId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement