第一步:注册权限
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> <uses-permission android:name="android.permission.READ_CONTACTS" />
第二步:接收的model类
public class ContactModel { private String name; private ArrayList<String > list; public String getName() { return name; } public void setName(String name) { this.name = name; } public ArrayList<String> getList() { return list; } public void setList(ArrayList<String> list) { this.list = list; } }
第三步:mian——activity
public class MainActivity extends AppCompatActivity { private Button button; private ListView listView; static final String TAG = "MainActivity"; ArrayList<ContactModel>dataList = new ArrayList<ContactModel>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView)findViewById(R.id.contrcts_view); readContacts(); MainAdapter adapter = new MainAdapter(this,dataList); listView.setAdapter(adapter); } private void readContacts(){ Cursor cursor = null; try { cursor = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); int contactIdIndex = 0; int nameIndex = 0; if(cursor.getCount() > 0) { contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID); nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); } ContactModel model = null; while(cursor.moveToNext()) { model = new ContactModel(); String contactId = cursor.getString(contactIdIndex); String name = cursor.getString(nameIndex); // Log.i(TAG, contactId); Log.i(TAG, "==============:" + name); model.setName(name); /* * 查找该联系人的phone信息 */ Cursor phones = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null); int phoneIndex = 0; if (phones.getCount() > 0) { phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); } ArrayList <String>list = new ArrayList<String>(); while (phones.moveToNext()) { String phoneNumber = phones.getString(phoneIndex); Log.i(TAG, "==============:" + phoneNumber); list.add(phoneNumber); } model.setList(list); dataList.add(model); } }catch (Exception e){ Log.e(TAG,e.toString()); }finally { if(cursor != null){ cursor.close(); } } } }
第四步:adapter
public class MainAdapter extends BaseAdapter { private Context context; private List<ContactModel> list; public MainAdapter(Context context, List<ContactModel>list){ this.context = context; this.list = list; } @Override public int getCount() { return this.list.size(); } @Override public Object getItem(int i) { return this.list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder; if(view == null){ view = LayoutInflater.from(this.context).inflate(R.layout.item_main,null); holder = new ViewHolder(); holder.nameTV = (TextView)view.findViewById(R.id.name); holder.phoneTV = (TextView)view.findViewById(R.id.phone); view.setTag(holder); }else{ holder = (ViewHolder)view.getTag(); } ContactModel model = this.list.get(i); holder.nameTV.setText(model.getName()); StringBuffer buffer = new StringBuffer(); for (int j=0;j<model.getList().size();j++){ if(j==0){ buffer.append(model.getList().get(j)); }else { buffer.append("\n"+model.getList().get(j)); } } holder.phoneTV.setText(buffer.toString()); return view; } class ViewHolder{ TextView nameTV; TextView phoneTV; } }
第五步:item 布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="tom" android:layout_margin="5dp" android:id="@+id/name"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="18580633453" android:layout_margin="5dp" android:id="@+id/phone"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#d6d6d6" /> </LinearLayout>
main的布局就一个listview,如果不会的话我需要静静了
时间: 2024-11-05 23:19:15