该Demo实战项目主要演示了如何从手机中获取联系人手机号及联系人名称。
Cursor cursor=context.getContentResolver().query(CoONTENT_URI,null,null,null,null);
cursor.moveToNext()
package com.mero.ceshi;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone;
public class GetPhoneContacts {
public static List list=new ArrayList();
public static String getNumber(Context context){
Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
String phoneNumber;
String phoneName;
//while(cursor.moveToNext()){}这两种遍历方式一样
for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
phoneName = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
System.out.println("phoneNumber:"+phoneNumber+"phoneName:"+phoneName);
PhoneEntity phoneEntity=new PhoneEntity(phoneNumber, phoneName);
list.add(phoneEntity);
}
return null;
}
package com.mero.ceshi;
public class PhoneEntity {
private String phoneNumber;//手机号
private String phoneName;//手机联系人
PhoneEntity(String phoneNumber,String phoneName){
this.phoneName=phoneName;
this.phoneNumber = phoneNumber;
}
/**
* @return the phoneNumber
*/
public String getPhoneNumber() {
return phoneNumber;
}
/**
* @param phoneNumber the phoneNumber to set
*/
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
/**
* @return the phoneName
*/
public String getPhoneName() {
return phoneName;
}
/**
* @param phoneName the phoneName to set
*/
public void setPhoneName(String phoneName) {
this.phoneName = phoneName;
}
}
package com.mero.ceshi;
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 Context context;
private List lists;
private LinearLayout layout;
public MyAdapter(Context context,List lists){
this.context=context;
this.lists=lists;
}
//返回列表的显示项,跟传进来的list数据数目一致。
@Override
public int getCount() {
return lists.size();
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItem(int)
*/
//返回当前项
@Override
public Object getItem(int position) {
return lists.get(position);
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItemId(int)
*/
//返回当前项的索引
@Override
public long getItemId(int position) {
return position;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
//得到布局视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//得到布局加载器
LayoutInflater inflater = LayoutInflater.from(context);
//加载得到item每一项的布局
layout = (LinearLayout) inflater.inflate(R.layout.list_item_phone, null);
//通过视图得到控件
TextView phoneNumTv = (TextView) layout.findViewById(R.id.tx_phoneNumber);
TextView phoneNameTv = (TextView) layout.findViewById(R.id.tx_phoneName);
//为控件填充数据
phoneNumTv.setText(lists.get(position).getPhoneNumber());
phoneNameTv.setText(lists.get(position).getPhoneName());
return layout;
}
}
接下来就是我们的item项的布局文件。记得把字符串提取到strings.xml文件中,快捷键提示是ctrl+1。
list_item_phone.xml
package com.mero.ceshi;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView ls;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetPhoneContacts.getNumber(this);//调用getNumber方法,将数据封装到List集合中。
ls=(ListView) findViewById(R.id.list_phone);//得到ListView对象
adapter=new MyAdapter(this, GetPhoneContacts.list);//得到适配器对象
ls.setAdapter(adapter);//为listView设置适配器
}
}