Android 系统给我们提供了访问通讯录的接口,通过接口获取通讯录信息。Adapter 与 View 的连接主要依靠 getView 这个方法返回我们需要的自定义 view。 ListView 是 Android App 中一个最最最常用的控件了,所以如何让 ListView 流畅运行,获取良好的用户体验是非常重要的。 对 ListView 优化就是对 Adapter 中的 getView 方法进行优化。
核心内容:
1.获取手机通讯录
2.数据封装
3.创建 Adapter
4.优化适配器
开发环境:Eclipse
效果图:
步骤:
1.新建项目,新建 GetNumber.class 类,用来获取通讯录中的信息
package com.yanis.getmyphonenumber; import java.util.ArrayList;
import java.util.List; import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone; public class GetNumber {
public static List<PhoneInfo> lists = new ArrayList<PhoneInfo>();
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()) {
phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));//电话号码
phoneName = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));//姓名
PhoneInfo info = new PhoneInfo(phoneName, phoneNumber);
lists.add(info);
System.out.println(phoneName+phoneNumber);
}
return null;
}
}
2.添加权限
<!-- 读取联系*限 -->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
3.新建 PhoneInfo.class 类作为通讯录信息封装类
package com.yanis.getmyphonenumber; /**
*
* @author yechao
* @说明 通讯录信息封装类
*/
public class PhoneInfo {
private String phoneName;
private String phoneNumber; public PhoneInfo(String phoneName, String phoneNumber) {
setPhoneName(phoneName);
setPhoneNumber(phoneNumber);
} public String getPhoneName() {
return phoneName;
} public void setPhoneName(String phoneName) {
this.phoneName = phoneName;
} public String getPhoneNumber() {
return phoneNumber;
} public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
} }
4.自定义 ListView 的适配器类 MyAdapter.class
package com.yanis.getmyphonenumber; 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 List<PhoneInfo> lists;
private Context context;//承接上下文 public MyAdapter(List<PhoneInfo> lists,Context context)
{
this.lists = lists;
this.context = context;
} //返回集合的数量
@Override
public int getCount() {
return lists.size();
} //返回当前数据
@Override
public Object getItem(int position) {
return lists.get(position);
} //获取当前ID
@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(context).inflate(R.layout.call,null);
holder = new ViewHolder();
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.tvName.setText(lists.get(position).getPhoneName());
holder.tvNumber.setText(lists.get(position).getPhoneNumber());
return convertView;
} private static class ViewHolder{
TextView tvName;
TextView tvNumber;
} }
5.最后就是主界面代码,至于布局太简单就不列出来了,有兴趣的童鞋可以查看源代码
package com.yanis.getmyphonenumber; import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView; public class MainActivity extends Activity {
private ListView listView;
private MyAdapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); GetNumber.getNumber(this); initView();
} private void initView() {
listView =(ListView) findViewById(R.id.listView);
adapter = new MyAdapter(GetNumber.lists, this);
listView.setAdapter(adapter);
}
}
源代码地址:https://github.com/YeXiaoChao/Yc_demo_getMyPhoneNumber