shlist

share and manage lists between multiple people
Log | Files | Refs

ContactsScreen.java (5383B)


      1 package drsocto.shlist;
      2 
      3 import android.content.Context;
      4 import android.content.Intent;
      5 import android.database.Cursor;
      6 import android.provider.ContactsContract;
      7 import android.support.v7.app.ActionBarActivity;
      8 import android.os.Bundle;
      9 import android.util.Log;
     10 import android.view.LayoutInflater;
     11 import android.view.Menu;
     12 import android.view.MenuItem;
     13 import android.view.View;
     14 import android.view.ViewGroup;
     15 import android.widget.ArrayAdapter;
     16 import android.widget.Button;
     17 import android.widget.CheckBox;
     18 import android.widget.ListView;
     19 import android.widget.TextView;
     20 
     21 import java.util.ArrayList;
     22 import java.util.regex.Matcher;
     23 import java.util.regex.Pattern;
     24 
     25 
     26 public class ContactsScreen extends ActionBarActivity {
     27 
     28     @Override
     29     protected void onCreate(Bundle savedInstanceState) {
     30         super.onCreate(savedInstanceState);
     31         setContentView(R.layout.layout_contacts_screen);
     32 
     33         ArrayList<String> list = new ArrayList<String>();
     34         ArrayList<Boolean> list_bool = new ArrayList<Boolean>();
     35         ListView lv = (ListView) findViewById(R.id.contactList);
     36 
     37         ArrayList<Contact> contacts_list = new ArrayList<Contact>();
     38 
     39         Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
     40         while (phones.moveToNext())
     41         {
     42             String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
     43             String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
     44             String pattern = "(\\d*)";
     45             Pattern r = Pattern.compile(pattern);
     46             Matcher m = r.matcher(phoneNumber);
     47             String out = "";
     48             while(m.find()) {
     49                 out += m.group(1);
     50             }
     51             if (out.length() == 11) {
     52                 out = out.substring(1);
     53             }
     54             Log.d("contacts", "Regex: " + out);
     55             phoneNumber = out;
     56 
     57             Contact contact = new Contact(name, phoneNumber);
     58             contacts_list.add(contact);
     59             list.add(name);
     60         }
     61         phones.close();
     62         MyContactsListsAdapter adapter = new MyContactsListsAdapter(this, R.id.contactCheckBox, contacts_list, list);
     63         lv.setAdapter(adapter);
     64     }
     65 
     66 
     67     @Override
     68     public boolean onCreateOptionsMenu(Menu menu) {
     69         // Inflate the menu; this adds items to the action bar if it is present.
     70         getMenuInflater().inflate(R.menu.menu_contacts_screen, menu);
     71         return true;
     72     }
     73 
     74     public void listPage(View v) {
     75         finish();
     76     }
     77 
     78     @Override
     79     public boolean onOptionsItemSelected(MenuItem item) {
     80         // Handle action bar item clicks here. The action bar will
     81         // automatically handle clicks on the Home/Up button, so long
     82         // as you specify a parent activity in AndroidManifest.xml.
     83         int id = item.getItemId();
     84 
     85         //noinspection SimplifiableIfStatement
     86         if (id == R.id.action_settings) {
     87             return true;
     88         }
     89 
     90         return super.onOptionsItemSelected(item);
     91     }
     92 
     93     private class MyContactsListsAdapter extends ArrayAdapter<String> {
     94 
     95         private ArrayList<Contact> contacts;
     96 
     97         public MyContactsListsAdapter(Context context, int textViewResourceId,
     98                                 ArrayList<Contact> taskList, ArrayList<String> stringList) {
     99             super(context, textViewResourceId, stringList);
    100             contacts = taskList;
    101         }
    102 
    103         private class ViewHolder {
    104             Contact shlist;
    105             CheckBox name;
    106         }
    107 
    108         public Contact getContact(int position) {
    109             return contacts.get(position);
    110         }
    111 
    112         @Override
    113         public View getView(final int position, View convertView, ViewGroup parent) {
    114 
    115             ViewHolder viewHolder = null;
    116             Log.v("ConvertView", String.valueOf(position));
    117 
    118             if (convertView == null) {
    119                 LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    120                 convertView = inflater.inflate(R.layout.contact_row, null);
    121 
    122                 viewHolder = new ViewHolder();
    123 
    124 
    125                 convertView.setTag(viewHolder);
    126 
    127             }
    128             else {
    129                 viewHolder = (ViewHolder) convertView.getTag();
    130             }
    131 
    132             viewHolder.shlist = contacts.get(position);
    133             viewHolder.name = (CheckBox) convertView.findViewById(R.id.contactCheckBox);
    134             viewHolder.name.setText(viewHolder.shlist.getName());
    135             viewHolder.name.setChecked(viewHolder.shlist.getSelected());
    136             final Contact contact = viewHolder.shlist;
    137             viewHolder.name.setOnClickListener(new View.OnClickListener() {
    138                 @Override
    139                 public void onClick(View v) {
    140                     Log.d("contacts", "Clicked: " + contact.getName() + ":" + contact.getNumber());
    141                     if (contact.getSelected()) {
    142                         contact.setSelected(false);
    143                     } else {
    144                         contact.setSelected(true);
    145                     }
    146                 }
    147             });
    148 
    149             return convertView;
    150 
    151         }
    152 
    153     }
    154 }