Android简单通讯录的开发

来源:互联网 发布:淘宝网购物ppt 编辑:程序博客网 时间:2024/06/05 19:06

本次开发分为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<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 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<PhoneInfo> lists;
private Context context;
private LinearLayout layout;

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


@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:

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


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


</RelativeLayout>

cell.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


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


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


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


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


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


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


</LinearLayout>

主类部分:

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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.getmyphonenumber"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
 <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>


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


2 0
原创粉丝点击