郭霖的第一行代码举了读取联系人的例子,前有打电话的例子,该结合在一起,点击联系人就好打电话。要响应点击单个联系人的事件。布局不改变,但是列表形式要改变:
android.R.layout.simple_list_item_2 或 android.R.layout.simple_expandable_list_item_2。
activity_main.xml
AndroidManifest.xml:
MainActivity.java:
package com.example.contactstest;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private Map
contact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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(){
listView = (ListView)findViewById(R.id.contacts_view);
SimpleAdapter simpleAdapter;
List