Put the phone Address Book in PinnedSectionListView, sort it by the first letter of the name, and enable the call function ., Dingtalk calls non-address book

Source: Internet
Author: User

Put the phone Address Book in PinnedSectionListView, sort it by the first letter of the name, and enable the call function ., Dingtalk calls non-address book

1 package com. lixu. tongxunlu; 2 3 import java. util. arrayList; 4 import com. lixu. tongxunlu. pinnedSectionListView. pinnedSectionListAdapter; 5 import android. app. activity; 6 import android. content. contentResolver; 7 import android. content. context; 8 import android. content. intent; 9 import android. database. cursor; 10 import android. graphics. color; 11 import android.net. uri; 12 import android. OS. bundle; 13 import android. provider. contactsContract; 14 import android. view. layoutInflater; 15 import android. view. view; 16 import android. view. viewGroup; 17 import android. view. window; 18 import android. view. windowManager; 19 import android. widget. adapterView; 20 import android. widget. adapterView. onItemClickListener; 21 import android. widget. arrayAdapter; 22 import android. widget. textView; 23 24 public Class MainActivity extends Activity {25 private ArrayList <Contacts> contacts; 26 private ArrayList <Data> data; 27 28 @ Override 29 protected void onCreate (Bundle savedInstanceState) {30 super. onCreate (savedInstanceState); 31 // remove the header 32 requestWindowFeature (Window. FEATURE_NO_TITLE); 33 setContentView (R. layout. activity_main); 34 // set the full screen of the program 35 toggleFullscreen (true); 36 37 data = new ArrayList <Data> (); 38 contacts = new ArrayList <Contacts> (); 39 // after obtaining the mobile phone contact, pack it to contacts. 40 getcontacts (contacts); 41 42 int A = (int) 'A'; 43 for (int I = 0; I <26; I ++) {44 int letter = A + I; 45 char c = (char) letter; 46 47 Data group = new Data (); 48 49 group. type = Data. GROUP; 50 group. text = c + ""; 51 data. add (group); 52 for (int j = 0; j <contacts. size (); j ++) {53 Contacts cc = contacts. get (j); 54 Stri Ng ss = cc. firstLetterofNmae (); 55 // determine whether the first letter of the contact name is the same as that of the group name, and then add it to the group 56 if (ss. equals (group. text) {57 Data child = new Data (); 58 child. type = Data. CHILD; 59 child. text = cc. name + "" + cc. getPhoneNumbers (); 60 child. contacts = cc; 61 data. add (child); 62} 63 64} 65 66} 67 68 PinnedSectionListView pslv = (PinnedSectionListView) findViewById (R. id. pslv); 69 70 pslv. setAdapter (new MyAdapter (this,-1 ); 71 72 pslv. setOnItemClickListener (new OnItemClickListener () {73 74 @ Override 75 public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {76 // click the submenu to trigger the Click Event 77 if (data. get (position ). type = Data. CHILD) {78 Data datas = data. get (position); 79 Contacts contact = datas. contacts; 80 // obtain the phone number 81 String number = contact. getPhoneNumbers (); 82 83 Intent intent = new Intent (Intent. ACTION_CALL, Uri. parse ("tel:" + number); 84 startActivity (intent); 85} 86} 87}); 88 89} 90 91 // Method 9 for setting full screen program 2 public void toggleFullscreen (boolean fullScreen) {93 // when fullScreen is true, the full screen 94 95 WindowManager is displayed. layoutParams attrs = getWindow (). getAttributes (); 96 97 if (fullScreen) {98 attrs. flags | = WindowManager. layoutParams. FLAG_FULLSCREEN; 99} else {100 attrs. flags & = ~ WindowManager. layoutParams. FLAG_FULLSCREEN; 101} 102 103 getWindow (). setAttributes (attrs); 104} 105 106 private class Data {107 public static final int GROUP = 0; 108 public static final int CHILD = 1; 109 public static final int STYE = 2; 110 public String text; 111 public int type; 112 public Contacts contacts = null; 113 114} 115 private class MyAdapter extends ArrayAdapter implements PinnedSectionListAdapter {116 private LayoutInflater flater; 118 119 public MyAdapter (Context context, int resource) {120 super (context, resource); 121 flater = LayoutInflater. from (context); 122} 123 124 @ Override125 public int getCount () {126 return data. size (); 127} 128 129 @ Override130 public Object getItem (int position) {131 return data. get (position); 132} 133 134 @ Override135 public int getItemViewType (int position) {136 return data. get (position ). type; 137} 138 139 @ Override140 public int getViewTypeCount () {141 return Data. STYE; 142} 143 144 @ Override145 public View getView (int position, View convertView, ViewGroup parent) {146 int type = getItemViewType (position); 147 Data data = (Data) getItem (position); 148 switch (type) {149 case Data. GROUP: 150 if (convertView = null) 151 convertView = flater. inflate (android. r. layout. simple_list_item_1, null); 152 153 TextView TV = (TextView) convertView. findViewById (android. r. id. text1); 154 TV. setBackgroundColor (Color. GREEN); 155 TV. setTextSize (35); 156 TV. setText (data. text); 157 158 159 break; 160 case Data. children: 161 if (convertView = null) 162 convertView = flater. inflate (android. r. layout. simple_list_item_1, null); 163 164 TextView tv1 = (TextView) convertView. findViewById (android. r. id. text1); 165 tv1.setTextSize (20); 166 tv1.setText (data. text); 167 168 169 break; 170 171 default: 172 break; 173} 174 return convertView; 175} 176 178 @ Override177 public boolean isItemViewTypePinned (int viewType) {boolean type = false; 179 switch (viewType) {180 case Data. CHILD: 181 182 type = false; 183 184 break; 185 186 case Data. GROUP: 187 188 type = true; 189 190 191 break; 192 193 default: 194 break; 195} return type; 196} 197 198} 199 200 // method for obtaining mobile phone Contacts 201 private void getcontacts (ArrayList <contacts> Contacts) {202 Uri uri = Uri. parse ("content: // com. android. contacts/contacts "); 203 ContentResolver resolver = this. getContentResolver (); 204 // pass a SORT_KEY_PRIMARY to the query, so that ContentResolver sorts the obtained data by the first letter of the contact name. 205 Cursor cursor = resolver. query (uri, null, 206 android. provider. contactsContract. contacts. SORT_KEY_PRIMARY); 207 while (cursor. moveToNext () {208 // The id209 String id of the contact = cursor. getString (cursor. getColumnIndex (android. provider. contactsContract. contacts. _ ID); 210 // group the contact by the first letter of the name 211 String sort_key_primary = cursor212. getString (cursor. getColumnIndex (android. provider. contactsContract. contacts. SORT_KEY_PRIMARY); 213 // obtain the contact name 214 String name = cursor215. getString (cursor. getColumnIndex (android. provider. contactsContract. contacts. DISPLAY_NAME); 216 Contacts mContacts = new Contacts (); 217 mContacts. id = id; 218 mContacts. name = name; 219 mContacts. sort_key_primary = sort_key_primary; 220 // obtain the contact's mobile phone number 221 Cursor phone = resolver. query (ContactsContract. commonDataKinds. phone. content_uris, null, 222 ContactsContract. commonDataKinds. phone. CONTACT_ID + "=" + id, null, null); 223 // traverse the obtained number to 224 ArrayList <String> phonenumbers = new ArrayList <String> (); 225 while (phone. moveToNext () {226 int phoneFieldColumnIndex = phone. getColumnIndex (ContactsContract. commonDataKinds. phone. NUMBER); 227 String phonenumber = phone. getString (phoneFieldColumnIndex); 228 phonenumbers. add (PhP number); 229} 230 mContacts. phonenumbers = phonenumbers; 231 contacts. add (mContacts); 232 233} 234} 235 236 private class Contacts {237 public String id; 238 public String name; 239 public String sort_key_primary; 240 public ArrayList <String> phonenumbers; 241 242 // get the first letter of a Chinese character and convert it to the uppercase letter 243 public String firstLetterofNmae () {244 String s = sort_key_primary.charAt (0) + "; 245 return s. toUpperCase (); 246 247} 248 249 public String getPhoneNumbers () {250 String phones = ""; 251 for (int I = 0; I <phonenumbers. size (); I ++) {252 phones + = ":" + phonenumbers. get (I); 253} 254 return phones; 255 256} 257} 258 259}

Xml file:

Do not forget to add various permissions.

 

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:padding="10dp" > 6  7     <com.lixu.tongxunlu.PinnedSectionListView 8         android:id="@+id/pslv" 9         android:layout_width="wrap_content"10         android:layout_height="wrap_content" />11 12 </RelativeLayout>

Result example:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.