本次开发分为4步:

1、获取手机通讯录的信息;

2、手机通讯录的数据封装;

3、手机通讯录的信息的UI适配;

4、对ListView的优化。

GetNumber.java:

用来获取手机通讯录。下面是代码部分:

package com.example.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 lists = 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()) {
phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
phoneName = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
PhoneInfo phoneInfo = new PhoneInfo(phoneName, phoneNumber);
lists.add(phoneInfo);
System.out.println(phoneName+phoneNumber);
}
return null;
}


}

2、PhoneInfo.java:

用来对姓名和电话号码进行数据封装。下面是代码部分:

package com.example.getmyphonenumber;


public class PhoneInfo {
private String name;
private String number;

public PhoneInfo(String name,String number) {
setNumber(number);
setName(name);
}

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;
}


}

3、Myadapter.java:

用来对手机通讯录信息进行UI适配和ListView优化。下面是代码部分:

package com.example.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 lists;
private Context context;
private LinearLayout layout;

public Myadapter(List 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);
}


@Override
public long getItemId(int position) {
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
// LayoutInflater inflater = LayoutInflater.from(context);
// layout = (LinearLayout) inflater.inflate(R.layout.call, null);
// TextView nametv = (TextView) layout.findViewById(R.id.name);
// TextView numbertv = (TextView) layout.findViewById(R.id.number);
// nametv.setText(lists.get(position).getName());
// numbertv.setText(lists.get(position).getNumber());
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.cell, null);
holder = new ViewHolder();
holder.nametv = (TextView) convertView.findViewById(R.id.name);
holder.numbertv = (TextView) convertView.findViewById(R.id.number);
holder.nametv.setText(lists.get(position).getName());
holder.numbertv.setText(lists.get(position).getNumber());
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
holder.nametv.setText(lists.get(position).getName());
holder.numbertv.setText(lists.get(position).getNumber());
}
return convertView;
}
private static class ViewHolder{
TextView nametv;
TextView numbertv;
}

}

界面部分代码:

activity_main.xml:

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jikexueyuan.getmyphonenumber.MainActivity" >


            android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        >
   


cell.xml:

<?xml version="1.0" encoding="utf-8"?>
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


                    android:id="@+id/imageView1"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:src="@drawable/ic_launcher" />


                    android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_vertical"
            android:orientation="horizontal" >


                            android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >


                                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:text="TextView" />


                                    android:id="@+id/number"
                    android:textSize="10sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextView" />
           

       
   




主类部分:

MainActivity.java:

package com.example.getmyphonenumber;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;




public class MainActivity extends Activity {

private ListView lv;
private Myadapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GetNumber.getNumber(this);
        lv = (ListView) findViewById(R.id.lv);
        adapter = new Myadapter(GetNumber.lists, this);
        lv.setAdapter(adapter);
    }
}

权限部分:

<?xml version="1.0" encoding="utf-8"?>
    package="com.example.getmyphonenumber"
    android:versionCode="1"
    android:versionName="1.0" >


            android:minSdkVersion="8"
        android:targetSdkVersion="21" />
 
            android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
                    android:name=".MainActivity"
            android:label="@string/app_name" >
           
               


               
           

       
   




总结:只是一个简单的Android通讯录,基本功能实现了,后期还可以有很多改进,比如加入E-mail和家庭住址等信息。


更多相关文章

  1. Android(安卓)开发TCP、UdP客户端
  2. android webview的 一些设置(js java交互)
  3. Android(安卓)Handler的使用yu应该注意的问题
  4. Android中ListView下拉刷新的实现代码
  5. Android控制手机振动相关
  6. Android(安卓)4.0 ICS SystemUI浅析——StatusBar加载流程分析
  7. Android与H5页面的互调
  8. Android周学习Step By Step(9)--Intent之广播(完)
  9. Android周学习Step By Step(8)--Intent之启动新的Activity

随机推荐

  1. Android应用于军事制造业,开放性优势受青
  2. 浅析Android单线程模型
  3. Android实践手册:该如何应用Android知识到
  4. Android(安卓)加载模型
  5. Android(安卓)API中常用的包
  6. Android必备知识(五)多线程及AsyncTask
  7. android wifi设置
  8. Android(安卓)Studio共用Eclipse的Androi
  9. Android面试题集2019版(包含答案整理)
  10. Android线程