Android ListView實現通訊錄的實例

該Demo實戰項目主要演示瞭如何從手機中獲取聯繫人手機號及聯繫人名稱。


  • ListView的自定義適配器使用方法
  • 內容解析器ContentResolver的使用方法
  • 數據封裝
內容比較簡單,但詳細地演示了把數據綁定到ListView並顯示出來的效果。

項目UI界面實現:ListView控件
項目邏輯實現:先獲取手機聯繫人及手機號碼信息,然後封裝到實體類中,最後將實體類數據通過ListView顯示

效果演示如下:

首先,我們先獲取聯繫人信息,需要獲取的有手機號和聯繫人。
第一步:獲取Cursor遊標對象

Cursor cursor=context.getContentResolver().query(CoONTENT_URI,null,null,null,null);

第二步:
通過遊標遍歷所有的數據

cursor.moveToNext()

獲取聯繫人代碼如下

GetPhoneContacts.java

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<PhoneEntity> list=new ArrayList<PhoneEntity>();
	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;
	}

通過以上代碼,我們使用getNumber方法把聯繫人和手機號封裝進了類型爲PhoneEntity的List中。
然後,看到我們的實體封裝類,分別實現其Getter和Setter方法。
PhoneEntity.java
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;
	}
}


接下來就是我們的適配器。我們採用的是繼承BaseAdapter。這樣就可以自己定義適配器的佈局了。創建一個MyAdapter
MyAdapter.java

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<PhoneEntity> lists;
	private LinearLayout layout;
	public MyAdapter(Context context,List<PhoneEntity> 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

<?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="vertical" >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView 
            android:id="@+id/image1"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:src="@drawable/ic_launcher"/>
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center_vertical">
            <TextView 
                android:id="@+id/tx_phoneName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="聯繫人"
                android:textSize="17sp"></TextView>
            <TextView android:id="@+id/tx_phoneNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="手機號碼"></TextView>
            </LinearLayout>
    </LinearLayout>

</LinearLayout>


最後,就是實例化我們的ListView並將適配器數據綁定到其上面去。代碼如下:

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<PhoneEntity>集合中。
        ls=(ListView) findViewById(R.id.list_phone);//得到ListView對象
        adapter=new MyAdapter(this, GetPhoneContacts.list);//得到適配器對象
        ls.setAdapter(adapter);//爲listView設置適配器
    }
}

好了,通過上面的演示,我們成功地實現了通訊錄的Demo。謝謝閱讀。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章