[Android Demo] simple mobile phone Address Book, androiddemo address book
The Android system provides an interface to access the address book and obtain the address book information through the interface. The connection between the Adapter and the View mainly relies on the getView method to return the custom view we need. ListView is one of the most commonly used controls in Android apps. Therefore, it is very important to get a good user experience to make ListView run smoothly. Optimizing ListView is to optimize the getView method in the Adapter.
Core content:
1. Get the mobile phone address book
2. Data encapsulation
3. Create an Adapter
4. optimized the adapter
Development Environment: Eclipse
:
Steps:
1. Create a project and create the GetNumber. class to obtain information in the address book.
Package com. yanis. getmyphonenumber; import java. util. arrayList; import java. util. list; import android. content. context; import android. database. cursor; import android. provider. contactsContract. commonDataKinds. phone; public class GetNumber {public static List <PhoneInfo> lists = new ArrayList <PhoneInfo> (); public static String getNumber (Context context) {Cursor cursor = context. getContentResolver (). query (Phone. CONTENT_URI, null, null); String phoneNumber; String phoneName; while (cursor. moveToNext () {phoneNumber = cursor. getString (cursor. getColumnIndex (Phone. NUMBER); // phone NUMBER phoneName = cursor. getString (cursor. getColumnIndex (Phone. DISPLAY_NAME); // name PhoneInfo info = new PhoneInfo (phoneName, phoneNumber); lists. add (info); System. out. println (phoneName + phoneNumber);} return null ;}}
2. Add Permissions
<! -- Read contact permissions --> <uses-permission android: name = "android. permission. READ_CONTACTS"/>
3. Create the PhoneInfo. class as the address book information encapsulation class.
Package com. yanis. getmyphonenumber;/***** @ author yechao * @ Address Book information encapsulation class */public class PhoneInfo {private String phoneName; private String phoneNumber; public PhoneInfo (String phoneName, String phoneNumber) {setPhoneName (phoneName); setPhoneNumber (phoneNumber);} public String getPhoneName () {return phoneName;} public void setPhoneName (String phoneName) {this. phoneName = phoneName;} public String getPhoneNumber () {return phoneNumber;} public void setPhoneNumber (String phoneNumber) {this. phoneNumber = phoneNumber ;}}
4. Custom ListView adapter class MyAdapter. class
Package com. yanis. getmyphonenumber; import java. util. list; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. linearLayout; import android. widget. textView; public class MyAdapter extends BaseAdapter {private List <PhoneInfo> lists; private Context context; // undertake Context public MyAdapter (List <PhoneInfo> lists, context Context) {this. lists = lists; this. context = context;} // number of returned sets @ Override public int getCount () {return lists. size () ;}// returns the current data @ Override public Object getItem (int position) {return lists. get (position) ;}// get the current ID @ Override public long getItemId (int position) {return position ;}// return the current View @ Override public View getView (int position, view convertView, ViewGroup parent) {ViewHolder holder; if (convertView = null) {convertView = LayoutInflater. from (context ). inflate (R. layout. call, null); holder = new ViewHolder (); holder. tvName = (TextView) convertView. findViewById (R. id. TV _Name); holder. tvNumber = (TextView) convertView. findViewById (R. id. TV _Number); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} holder. tvName. setText (lists. get (position ). getPhoneName (); holder. tvNumber. setText (lists. get (position ). getPhoneNumber (); return convertView;} private static class ViewHolder {TextView tvName; TextView tvNumber ;}}
5. The last step is the main interface code. The layout is too simple to list. For more information, see the source code.
package com.yanis.getmyphonenumber;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;public class MainActivity extends Activity { private ListView listView; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GetNumber.getNumber(this); initView(); } private void initView() { listView =(ListView) findViewById(R.id.listView); adapter = new MyAdapter(GetNumber.lists, this); listView.setAdapter(adapter); }}
Source code: https://github.com/YeXiaoChao/GetMyPhoneNumber
Address: http://www.cnblogs.com/yc-755909659/p/4312221.html