首页 > 代码库 > Android简单获得通讯录

Android简单获得通讯录

ContactActivity:

 1 package com.example.practice5_service; 2  3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7  8 import com.example.adapter.simpleAdapter; 9 10 import android.app.Activity;11 import android.content.ContentResolver;12 import android.database.Cursor;13 import android.net.Uri;14 import android.os.Bundle;15 import android.provider.CallLog;16 import android.support.v4.widget.SimpleCursorAdapter;17 import android.view.Menu;18 import android.view.MenuItem;19 import android.widget.ListAdapter;20 import android.widget.ListView;21 22 public class ContactActivity extends Activity {23     private ListView phone_listview;24     private List<Map<String,String>> addressList;25     private simpleAdapter adapter;26     27     private ListAdapter listAdaper;28     private ContentResolver resolver;29     @Override30     protected void onCreate(Bundle savedInstanceState) {31         super.onCreate(savedInstanceState);32         setContentView(R.layout.activity_contact);33         initView();34         loadData();35     }36 37     public void initView() {38         phone_listview=(ListView) findViewById(R.id.phone_listview);39         addressList=new ArrayList<Map<String,String>>();40         adapter=new simpleAdapter(this,addressList,R.layout.phone_message,41                 new String[]{"id","name","phoneNum"},42                 new int[]{R.id.phone_id,R.id.name_phone,R.id.num_phone});43         44         phone_listview.setAdapter(adapter);45     }46     public void loadData(){47         //内容接受者取得内容提供者中的数据48         resolver=super.getContentResolver();//获得ContentResolver,有增删改查的方法        49         Cursor result=resolver.query(CallLog.Calls.CONTENT_URI, null, null, null, null);//获得CallLog.CONTENT_URI对应的信息50         while(result.moveToNext()){51             Map<String,String> row=new HashMap<String, String>();52             int id=result.getInt(result.getColumnIndex(CallLog.Calls._ID));53             String name=result.getString(result.getColumnIndex(CallLog.Calls.CACHED_NAME));54             String phoneNum=result.getString(result.getColumnIndex(CallLog.Calls.NUMBER));55             row.put("id", String.valueOf(id));56             row.put("name", name);57             row.put("phoneNum", phoneNum);58             addressList.add(row);59         }60         result.close();61         adapter.notifyDataSetChanged();62     }63 }

simpleAdapter:

 1 package com.example.adapter; 2  3 import java.util.List; 4 import java.util.Map; 5  6 import android.content.Context; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.BaseAdapter;10 import android.widget.SimpleAdapter;11 12 public class simpleAdapter extends SimpleAdapter {13 14     public simpleAdapter(Context context, List<? extends Map<String, ?>> data,15             int resource, String[] from, int[] to) {16         super(context, data, resource, from, to);17         // TODO Auto-generated constructor stub18     }19 }

 

Android简单获得通讯录