Main
private ListView lv; private List<Contact> datas = new ArrayList<Contact>(); private int[] headIds = {R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08, R.drawable.img09}; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "选择联系人:" + datas.get(position).getName(), Toast.LENGTH_SHORT).show(); } }); if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest. permission.READ_CONTACTS}, 1); } else { readContacts(); } } private void readContacts() { Cursor cursor = null; ContentResolver resolver = getContentResolver(); try { cursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{"display_name", ContactsContract. CommonDataKinds.Phone.NUMBER}, null, null, null); if (cursor != null && cursor.getCount() > 0) { Contact contact = null; int i = 0; while (cursor.moveToNext()) { contact = new Contact(); String displayName = cursor.getString(0); String number = cursor.getString(1); contact.setHeadImaId(headIds[i++]); contact.setName(displayName); contact.setNumber(number); datas.add(contact); } adapter = new MyAdapter(); lv.setAdapter(adapter); } } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); } } } public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions, @NonNull int[] grantResults){ switch(requestCode){ case 1: if(grantResults.length>0&& grantResults[0]==PackageManager.PERMISSION_GRANTED){ readContacts(); }else{ Toast.makeText(this,"你拒绝了权限",Toast.LENGTH_SHORT).show(); } break; } } class MyAdapter extends BaseAdapter { @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return datas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_layout, null); holder = new ViewHolder(); holder.ivHead = (ImageView) convertView.findViewById(R.id.iv_head); 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.ivHead.setImageResource(datas.get(position).getHeadImaId()); holder.tvName.setText(datas.get(position).getName()); holder.tvNumber.setText(datas.get(position).getNumber()); return convertView; } class ViewHolder { ImageView ivHead; TextView tvName; TextView tvNumber; } } }
Contact
public class Contact { private int headImaId; private String name; private String number; public int getHeadImaId(){ return headImaId; } public void setHeadImaId(int headImaId){ this.headImaId=headImaId; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public String getNumber(){ return number; } public void setNumber(String number){ this.number=number; } }
main
android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
List
android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:padding="5dp"> <ImageView android:id="@+id/iv_head" android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/img01"/> <LinearLayout android:layout_width="224dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:orientation="vertical"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:text="Teacher Feng" android:textColor="#000000" android:textSize="20sp" /> <TextView android:id="@+id/tv_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="15456846665" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout>
<uses-permission android:name="android.permission.READ_CALL_LOG" /> <uses-permission android:name="android.permission.READ_CONTACTS"/>
android.enableAapt2=false