Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add the choice of a background color to the settings activity. Create a couple o

ID: 3853575 • Letter: A

Question

Add the choice of a background color to the settings activity. Create a couple of new color resource in color xml. Add these choices as a RadioGroup to the settings screen. You will have to modify the layout to place all the RadioGroups in a ScrollView so that you can see them all. Make the choice persist in a SharedPreferences object. Use the following command in the onCreate method of the setting activity to set the chosen background color: scrollviewobject.setBackgroundResource (R.color.colorresourcename): Create a method in ContactDatasource that will only update the Contact's Address. Create a ContactAddress object to pass data to the method. Modify the Contact table to include a field BFF that is an integer data type. Modify the onUpgrade method of ContactDBHelper to insert this new field without losing the data that is currently in the table.

Explanation / Answer

Contact.java


import android.text.format.Time;

public class Contact {
   private int contactID;
   private String contactName;
   private String streetAddress;
   private String city;
   private String state;
   private String zipCode;
   private String phoneNumber;
   private String cellNumber;
   private String email;
   private Time birthday;
  
   public Contact(){
       contactID = -1;
       Time t = new Time();
       t.setToNow();
       birthday = t;
   }

   public int getContactID(){
       return contactID;
   }
   public void setContactID(int i){
       contactID = i;
   }
   public String getContactName(){
       return contactName;
   }
   public void setContactName(String s){
       contactName = s;
   }
   public Time getBirthday(){
       return birthday;
   }
   public void setBirthday(Time t){
       birthday = t;
   }
   public String getStreetAddress(){
       return streetAddress;
   }
   public void setStreetAddress(String s){
       streetAddress = s;
   }
   public String getCity(){
       return city;
   }
   public void setCity(String s){
       city = s;
   }
   public String getState(){
       return state;
   }
   public void setState(String s){
       state = s;
   }
   public String getZipCode(){
       return zipCode;
   }
   public void setZipCode(String s){
       zipCode = s;
   }
   public String getPhoneNumber(){
       return phoneNumber;
   }
   public void setPhoneNumber(String s){
       phoneNumber = s;
   }
   public String getCellNumber(){
       return cellNumber;
   }
   public void setCellNumber(String s){
       cellNumber = s;
   }
   public String getEMail(){
       return email;
   }
   public void setEMail(String s){
       email = s;
   }
}

ContactActivity.java

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.telephony.PhoneNumberFormattingTextWatcher;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.text.style.BackgroundColorSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.example.mycontactlist.DatePickerDialog.SaveDateListener;

public class ContactActivity extends FragmentActivity implements SaveDateListener {

   private Contact currentContact;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);
      
        initListButton();
        initMapButton();
        initSettingsButton();
        initToggleButton();
        initSaveButton();
        setForEditing(false);
      
        initChangeDateButton();
        initTextChangedEvents();
        currentContact = new Contact();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.contact, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initListButton(){
       ImageButton list = (ImageButton)findViewById(R.id.imageButtonList);
       list.setOnClickListener(new View.OnClickListener() {
           public void onClick (View v){
               Intent intent = new Intent(ContactActivity.this, ContactListActivity.class);
               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               startActivity(intent);
           }
       });
    }
  
    private void initMapButton(){
       ImageButton map = (ImageButton)findViewById(R.id.imageButtonMap);
       map.setOnClickListener(new View.OnClickListener() {
           public void onClick (View v){
               Intent intent = new Intent(ContactActivity.this, ContactMapActivity.class);
               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               startActivity(intent);
           }
       });
    }
  
    private void initSettingsButton(){
       ImageButton settings = (ImageButton)findViewById(R.id.imageButtonSettings);
       settings.setOnClickListener(new View.OnClickListener() {
           public void onClick (View v){
               Intent intent = new Intent(ContactActivity.this, ContactSettingsActivity.class);
               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               startActivity(intent);
           }
       });
    }
  
    private void initToggleButton(){
       final ToggleButton editToggle = (ToggleButton)findViewById(R.id.toggleButtonEdit);
       editToggle.setOnClickListener(new OnClickListener(){
          
           @Override
           public void onClick(View arg0){
               setForEditing(editToggle.isChecked());
            }
       });
    }
  
    private void setForEditing(boolean enabled){
       EditText editName = (EditText)findViewById(R.id.editName);
       EditText editAddress = (EditText)findViewById(R.id.editAddress);
       EditText editCity = (EditText)findViewById(R.id.editCity);
       EditText editState = (EditText)findViewById(R.id.editState);
       EditText editZipCode = (EditText)findViewById(R.id.editZipcode);
       EditText editPhone = (EditText)findViewById(R.id.editHome);
       EditText editCell = (EditText)findViewById(R.id.editCell);
       EditText editEmail = (EditText)findViewById(R.id.editEMail);
       Button buttonChange = (Button)findViewById(R.id.btnBirthday);
       Button buttonSave = (Button)findViewById(R.id.buttonSave);
             
       editName.setEnabled(enabled);
       editAddress.setEnabled(enabled);
       editCity.setEnabled(enabled);
       editState.setEnabled(enabled);
       editZipCode.setEnabled(enabled);
       editPhone.setEnabled(enabled);
       editCell.setEnabled(enabled);
       editEmail.setEnabled(enabled);
       buttonChange.setEnabled(enabled);
       buttonSave.setEnabled(enabled);
      
       if(enabled){
           editName.requestFocus();
           editName.setBackgroundColor(getResources().getColor(R.color.dataEntry_background));
           editAddress.setBackgroundColor(getResources().getColor(R.color.dataEntry_background));
           editCity.setBackgroundColor(getResources().getColor(R.color.dataEntry_background));
           editState.setBackgroundColor(getResources().getColor(R.color.dataEntry_background));
           editZipCode.setBackgroundColor(getResources().getColor(R.color.dataEntry_background));
           editPhone.setBackgroundColor(getResources().getColor(R.color.dataEntry_background));
           editCell.setBackgroundColor(getResources().getColor(R.color.dataEntry_background));
           editEmail.setBackgroundColor(getResources().getColor(R.color.dataEntry_background));
       }
       else{
           ScrollView s = (ScrollView)findViewById(R.id.scrollView1);
           s.fullScroll(ScrollView.FOCUS_UP);
           s.clearFocus();
           editName.setBackgroundResource(android.R.drawable.editbox_background_normal);
           editAddress.setBackgroundResource(android.R.drawable.editbox_background_normal);
           editCity.setBackgroundResource(android.R.drawable.editbox_background_normal);
           editState.setBackgroundResource(android.R.drawable.editbox_background_normal);
           editZipCode.setBackgroundResource(android.R.drawable.editbox_background_normal);
           editPhone.setBackgroundResource(android.R.drawable.editbox_background_normal);
           editCell.setBackgroundResource(android.R.drawable.editbox_background_normal);
           editEmail.setBackgroundResource(android.R.drawable.editbox_background_normal);
       }
    }
  
    private void hideKeyboard(){
       InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
       EditText editName = (EditText)findViewById(R.id.editName);
       imm.hideSoftInputFromWindow(editName.getWindowToken(), 0);
       EditText editAddress = (EditText)findViewById(R.id.editAddress);
       imm.hideSoftInputFromInputMethod(editAddress.getWindowToken(), 0);
       EditText editCity = (EditText)findViewById(R.id.editCity);
       imm.hideSoftInputFromInputMethod(editCity.getWindowToken(), 0);
       EditText editState = (EditText)findViewById(R.id.editState);
       imm.hideSoftInputFromInputMethod(editState.getWindowToken(), 0);
       EditText editZipcode = (EditText)findViewById(R.id.editZipcode);
       imm.hideSoftInputFromInputMethod(editZipcode.getWindowToken(), 0);
       EditText editHomePhone = (EditText)findViewById(R.id.editHome);
       imm.hideSoftInputFromInputMethod(editHomePhone.getWindowToken(), 0);
       EditText editCellPhone = (EditText)findViewById(R.id.editCell);
       imm.hideSoftInputFromInputMethod(editCellPhone.getWindowToken(), 0);
       EditText editEmail = (EditText)findViewById(R.id.editEMail);
       imm.hideSoftInputFromInputMethod(editEmail.getWindowToken(), 0);
    }
  
   @Override
   public void didFinishDatePickerDialog(Time selectedTime) {
       TextView birthDay = (TextView)findViewById(R.id.textBirthday);
       birthDay.setText(DateFormat.format("MM/dd/yyyy", selectedTime.toMillis(false)).toString());
       currentContact.setBirthday(selectedTime);
   }
  
   private void initChangeDateButton(){
       Button changeDate = (Button)findViewById(R.id.btnBirthday);
       changeDate.setOnClickListener(new OnClickListener(){
           @Override
           public void onClick(View v){
               FragmentManager fm = getSupportFragmentManager();
               DatePickerDialog datePickerDialog = new DatePickerDialog();
               datePickerDialog.show(fm,"DatePick");
           }
       });
   }
  
   private void initSaveButton(){
       Button saveButton = (Button)findViewById(R.id.buttonSave);
       saveButton.setOnClickListener(new View.OnClickListener() {
          
           @Override
           public void onClick(View v){
               hideKeyboard();
               ContactDataSource ds = new ContactDataSource(ContactActivity.this);
               ds.open();
              
               boolean wasSuccessful = false;
               if(currentContact.getContactID()==-1){
                   wasSuccessful = ds.insertContact(currentContact);
                   int newId = ds.getLastContactId();
                   currentContact.setContactID(newId);
               }
               else {
                   wasSuccessful = ds.updateContact(currentContact);
               }
               ds.close();
              
                if(wasSuccessful){
                   ToggleButton editToggle = (ToggleButton)findViewById(R.id.toggleButtonEdit);
                   editToggle.toggle();
                   setForEditing(false);
               }
           }
       });
   }
  
    private void initTextChangedEvents(){
       final EditText contactName = (EditText)findViewById(R.id.editName);
       contactName.addTextChangedListener(new TextWatcher(){
           public void afterTextChanged(Editable s){
               currentContact.setContactName(contactName.getText().toString());
           }
           public void beforeTextChanged(CharSequence arg0, int ar1, int arg2, int arg3){
               // auto-generated method stub?
           }
           public void onTextChanged(CharSequence s, int start, int before, int count){
               // auto-generated method stub?
           }
       });
       final EditText streetAddress = (EditText)findViewById(R.id.editAddress);
       streetAddress.addTextChangedListener(new TextWatcher(){
           public void afterTextChanged(Editable s){
               currentContact.setStreetAddress(streetAddress.getText().toString());
           }
           public void beforeTextChanged(CharSequence arg0, int ar1, int arg2, int arg3){
               // auto-generated method stub?
           }
           public void onTextChanged(CharSequence s, int start, int before, int count){
               // auto-generated method stub?
           }
       });
       final EditText city = (EditText)findViewById(R.id.editCity);
       city.addTextChangedListener(new TextWatcher(){
           public void afterTextChanged(Editable s){
               currentContact.setCity(city.getText().toString());
           }
           public void beforeTextChanged(CharSequence arg0, int ar1, int arg2, int arg3){
               // auto-generated method stub?
           }
           public void onTextChanged(CharSequence s, int start, int before, int count){
               // auto-generated method stub?
           }
       });
       final EditText state = (EditText)findViewById(R.id.editState);
       state.addTextChangedListener(new TextWatcher(){
           public void afterTextChanged(Editable s){
               currentContact.setState(state.getText().toString());
           }
           public void beforeTextChanged(CharSequence arg0, int ar1, int arg2, int arg3){
               // auto-generated method stub?
           }
           public void onTextChanged(CharSequence s, int start, int before, int count){
               // auto-generated method stub?
           }
       });
       final EditText zipCode = (EditText)findViewById(R.id.editZipcode);
       zipCode.addTextChangedListener(new TextWatcher(){
           public void afterTextChanged(Editable s){
               currentContact.setZipCode(zipCode.getText().toString());
           }
           public void beforeTextChanged(CharSequence arg0, int ar1, int arg2, int arg3){
               // auto-generated method stub?
           }
           public void onTextChanged(CharSequence s, int start, int before, int count){
               // auto-generated method stub?
           }
       });
       final EditText email = (EditText)findViewById(R.id.editEMail);
       zipCode.addTextChangedListener(new TextWatcher(){
           public void afterTextChanged(Editable s){
               currentContact.setEMail(email.getText().toString());
           }
           public void beforeTextChanged(CharSequence arg0, int ar1, int arg2, int arg3){
               // auto-generated method stub?
           }
           public void onTextChanged(CharSequence s, int start, int before, int count){
               // auto-generated method stub?
           }
       });
       final EditText homephone = (EditText)findViewById(R.id.editHome);
       homephone.addTextChangedListener(new TextWatcher(){
           public void afterTextChanged(Editable s){
               currentContact.setPhoneNumber(homephone.getText().toString());
           }
           public void beforeTextChanged(CharSequence arg0, int ar1, int arg2, int arg3){
               // auto-generated method stub?
           }
           public void onTextChanged(CharSequence s, int start, int before, int count){
               // auto-generated method stub?
           }
       });
       final EditText cellphone = (EditText)findViewById(R.id.editCell);
       cellphone.addTextChangedListener(new TextWatcher(){
           public void afterTextChanged(Editable s){
               currentContact.setCellNumber(cellphone.getText().toString());
           }
           public void beforeTextChanged(CharSequence arg0, int ar1, int arg2, int arg3){
               // auto-generated method stub?
           }
           public void onTextChanged(CharSequence s, int start, int before, int count){
               // auto-generated method stub?
           }
       });
           homephone.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
           cellphone.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
    }

}

ContactAdapter.java


import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class ContactAdapter extends ArrayAdapter<Contact>{
   private ArrayList<Contact> items;
   private Context adapterContext;
  
   public ContactAdapter (Context context, ArrayList<Contact> items){
       super(context, R.layout.list_item, items);
        adapterContext = context;
       this.items = items;
   }
  
   @Override
   public View getView(int position, View convertView, ViewGroup parent){
       View v = convertView;
       try{
           Contact contact = items.get(position);
          
           if(v == null){
               LayoutInflater vi = (LayoutInflater)adapterContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
               v = vi.inflate(R.layout.list_item, null);
           }
          
           TextView contactName = (TextView)v.findViewById(R.id.textContactName);
           TextView contactNumber = (TextView)v.findViewById(R.id.textPhoneNumber);
           Button b = (Button)v.findViewById(R.id.buttonDeleteContact);
           contactName.setText(contact.getContactName());
           contactNumber.setText(contact.getPhoneNumber());
           b.setVisibility(View.INVISIBLE);
       }
       catch(Exception e){
           e.printStackTrace();
           e.getCause();
       }
       return v;
   }
   public void showDelete(final int position, final View convertView, final Context context, final Contact contact){
       View v = convertView;
       final Button b = (Button)v.findViewById(R.id.buttonDeleteContact);
      
       if(b.getVisibility()==View.INVISIBLE){
           b.setVisibility(View.VISIBLE);
           b.setOnClickListener(new View.OnClickListener(){
               @Override
               public void onClick(View v){
                   hideDelete(position,convertView,context);
                   items.remove(contact);
                   deleteOption(contact.getContactID(),context);
               }
           });
       }
       else{
           hideDelete(position,convertView,context);
       }
   }
  
   private void deleteOption(int contactToDelete,Context context){
       ContactDataSource db = new ContactDataSource(context);
       db.open();
       db.deleteContact(contactToDelete);
       db.close();
       this.notifyDataSetChanged();
   }
  
   public void hideDelete(int position, View convertView, Context context){
       View v = convertView;
       final Button b = (Button) v.findViewById(R.id.buttonDeleteContact);
       b.setVisibility(View.INVISIBLE);
       b.setOnClickListener(null);
   }
}

ContactAddress.java

public class ContactAddress {

   private String streetAddress;

   public void setAddress(String s)
   {
       streetAddress = s;
   }
   public String getAddress(){
       return streetAddress;
   }
}

ContactDBHelper.java


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ContactDBHelper extends SQLiteOpenHelper{
  
   private static final String DATABASE_NAME = "mycontacts.db";
   private static final int DATABASE_VERSION = 2;
  
   // Database creation sql statement
   private static final String CREATE_TABLE_CONTACT = "create table contact (_id integer primary key autoincrement, "
       + "contactname text not null, streetaddress text, "
       + "city text, state text, zipcode text, "
       + "phonenumber text, cellnumber text, "
       + "email text, birthday text, BFF integer);";
  
   public ContactDBHelper(Context context) {
       super(context, DATABASE_NAME, null, DATABASE_VERSION);
   }
  
   @Override
   public void onCreate(SQLiteDatabase database){
       database.execSQL(CREATE_TABLE_CONTACT);
   }
  
   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
       Log.w(ContactDBHelper.class.getName(),
               "Upgrading database from version " + oldVersion + " to "
               + newVersion + ", which should save all old data");
       db.execSQL("ALTER TABLE contact ADD COLUMN BFF integer");
       //onCreate(db);
   }
}


ContactDataSource.java


import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.Time;
import android.util.Log;

public class ContactDataSource {
   private SQLiteDatabase database;
   private ContactDBHelper dbHelper;
  
   public ContactDataSource(Context context){
       dbHelper = new ContactDBHelper(context);
   }
  
   public void open() throws SQLException{
       database = dbHelper.getWritableDatabase();
   }
  
   public void close(){
       dbHelper.close();
   }

   public boolean insertContact(Contact c){
       boolean didSucceed = false;
       try{
           ContentValues initialValues = new ContentValues();
          
           initialValues.put("contactname", c.getContactName());
           initialValues.put("streetaddress", c.getStreetAddress());
           initialValues.put("city", c.getCity());
           initialValues.put("state", c.getState());
           initialValues.put("zipcode", c.getZipCode());
           initialValues.put("phonenumber", c.getPhoneNumber());
           initialValues.put("cellnumber", c.getCellNumber());
           initialValues.put("email", c.getEMail());
           initialValues.put("birthday", String.valueOf(c.getBirthday().toMillis(false)));
          
           didSucceed = database.insert("contact", null, initialValues) > 0;
       }
       catch (Exception e){
           // Do nothing---will return false if there is an exception
       }
       return didSucceed;
   }

   public boolean updateContact(Contact c){
       boolean didSucceed = false;
       try{
           Long rowId = Long.valueOf(c.getContactID());
           ContentValues updateValues = new ContentValues();
          
           updateValues.put("contactname", c.getContactName());
           updateValues.put("streetaddress", c.getStreetAddress());
           updateValues.put("city", c.getCity());
           updateValues.put("state", c.getState());
           updateValues.put("zipcode", c.getZipCode());
           updateValues.put("phonenumber", c.getPhoneNumber());
           updateValues.put("cellnumber", c.getCellNumber());
           updateValues.put("email", c.getEMail());
           updateValues.put("birthday", String.valueOf(c.getBirthday().toMillis(false)));
          
           didSucceed = database.update("contact", updateValues, "_id=" + rowId, null) > 0;
       }
       catch (Exception e){
           // Do nothing---will return false if exception
       }
       return didSucceed;
   }
  
   public boolean updateAddress(Contact c, ContactAddress cAdd){
       boolean didSucceed = false;
       try{
           Long rowId = Long.valueOf(c.getContactID());
           ContentValues updateValues = new ContentValues();
          
           updateValues.put("streetaddress", cAdd.getAddress());
  
           didSucceed = database.update("contact", updateValues, "_id=" + rowId, null) > 0;
       }
       catch (Exception e){
           // Do nothing---will return false if exception
       }
       return didSucceed;
   }
  
   public int getLastContactId(){
       int lastId = -1;
       try{
           String query = "Select MAX(_id) from contact";
           Cursor cursor = database.rawQuery(query, null);
          
           cursor.moveToFirst();
           lastId = cursor.getInt(0);
           cursor.close();
       }
       catch (Exception e){
           lastId = -1;
       }
       return lastId;
   }
   public String getlastContactName(){
       String contactName = "";
       String streetAddress = "";
       String query = "SELECT contactname, streetaddress FROM contact";      
       try{
           Cursor cursor = database.rawQuery(query, null);
           cursor.moveToFirst();
          
           while(!cursor.isAfterLast()){
               contactName = cursor.getString(0);
               streetAddress = cursor.getString(1);
               cursor.moveToNext();
           }
           cursor.close();
       }
       catch(Exception e){
          
       }
       return contactName + ", " + streetAddress;
   }
   public ArrayList<String> getContactName(){
       ArrayList<String> contactNames = new ArrayList<String>();
       try{
           String query = "Select contactname from contact";
           Cursor cursor = database.rawQuery(query, null);
          
           cursor.moveToFirst();
           while(!cursor.isAfterLast()){
               contactNames.add(cursor.getString(0));
               cursor.moveToNext();
           }
           cursor.close();
       }
       catch(Exception e){
           contactNames = new ArrayList<String>();
       }
       return contactNames;
   }
  
   public ArrayList<Contact>getContacts(){
       ArrayList<Contact>contacts = new ArrayList<Contact>();
       try{
           String query = "SELECT * FROM contact";
           Cursor cursor = database.rawQuery(query, null);
          
           Contact newContact;
           cursor.moveToFirst();
           while(!cursor.isAfterLast()){
               newContact = new Contact();
               newContact.setContactID(cursor.getInt(0));
               newContact.setContactName(cursor.getString(1));
               newContact.setStreetAddress(cursor.getString(2));
               newContact.setCity(cursor.getString(3));
               newContact.setState(cursor.getString(4));
               newContact.setZipCode(cursor.getString(5));
               newContact.setPhoneNumber(cursor.getString(6));
               newContact.setCellNumber(cursor.getString(7));
               newContact.setEMail(cursor.getString(8));
               Time t = new Time();
               t.set(Long.valueOf(cursor.getString(9)));
               newContact.setBirthday(t);
              
               contacts.add(newContact);
               cursor.moveToNext();
           }
           cursor.close();
       }
       catch(Exception e){
           contacts = new ArrayList<Contact>();
       }
       return contacts;
   }
   public boolean deleteContact(int contactId){
       boolean didDelete = false;
       try{
           didDelete = database.delete("contact", "_id" + contactId, null) > 0;
       }
       catch(Exception e){
           //do nothing = return value already false
       }
       return didDelete;
   }
  
}

ContactListActivity.java


import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

public class ContactListActivity extends ListActivity {

   boolean isDeleting = false;
   ContactAdapter adapter;
  
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_contact_list);
       initListButton();
        initMapButton();
        initSettingsButton();
        initDeleteButton();
           
        ContactDataSource ds = new ContactDataSource(this);
        ds.open();
        final ArrayList<Contact>contacts = ds.getContacts();
        ds.close();
      
        adapter = new ContactAdapter(this, contacts);
        setListAdapter(adapter);
  
        ListView listView = getListView();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent,View itemClicked, int position, long id){
               Contact selectedContact = contacts.get(position);
               Intent intent = new Intent(ContactListActivity.this,ContactActivity.class);
               intent.putExtra("contactid", selectedContact.getContactID());
               startActivity(intent);
           }
       });
   }
  
   private void initDeleteButton(){
       final Button deleteButton = (Button)findViewById(R.id.ButtonDelete);
       deleteButton.setOnClickListener(new View.OnClickListener(){
           public void onClick(View v){
               if(isDeleting){
                   deleteButton.setText("Delete");
                   isDeleting = false;
               }
               else{
                   deleteButton.setText("Done Deleting");
                   isDeleting = true;
               }
           }
       });
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.contact_list, menu);
       return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       int id = item.getItemId();
       if (id == R.id.action_settings) {
           return true;
       }
       return super.onOptionsItemSelected(item);
   }
  
    private void initListButton(){
       ImageButton list = (ImageButton)findViewById(R.id.imageButtonList);
       list.setEnabled(false);
    }
  
    private void initMapButton(){
       ImageButton map = (ImageButton)findViewById(R.id.imageButtonMap);
       map.setOnClickListener(new View.OnClickListener() {
           public void onClick (View v){
               Intent intent = new Intent(ContactListActivity.this, ContactMapActivity.class);
               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               startActivity(intent);
           }
       });
    }
  
    private void initSettingsButton(){
       ImageButton settings = (ImageButton)findViewById(R.id.imageButtonSettings);
       settings.setOnClickListener(new View.OnClickListener() {
           public void onClick (View v){
               Intent intent = new Intent(ContactListActivity.this, ContactSettingsActivity.class);
               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               startActivity(intent);
           }
       });
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote