開發android應用,肯定會經常用到andorid手機連絡人,在android中一般都用Listview呈現手機連絡人,如果想實現用checkbox實現全選的效果,預設的ListView好像不太好解決這個問題。
以下步驟,可以使用自訂布局來實現手機連絡人的全選,效果如
1.建立包含Listview的主介面布局檔案main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout android:gravity="bottom"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@+id/lvContact"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
<ScrollView android:gravity="bottom"
android:id="@+id/scroll_bottom" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/rlall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:paddingBottom="0.0dip" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox android:id="@+id/cbSelectAll"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="全選" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
2.建立包含單個連絡人資訊的布局檔案contactlistitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/contact_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
/>
<CheckBox android:id="@+id/multiple_checkbox"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
3.主程式,所有的程式說明都在程式中做了注釋
package com.demo;
import java.util.HashMap;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class main extends Activity {
private CheckBox cbSelectAll;
boolean blCkAll=false;
private MyAdapter madapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//載入主布局介面檔案
setTitle("連絡人清單");
ListView listView = (ListView)findViewById(R.id.lvContact); //得到ListView;
cbSelectAll = (CheckBox) findViewById(R.id.cbSelectAll);//得到全選的CheckBox
ContentResolver contentResolver = this.getContentResolver();
Uri uri = Uri.parse("content://com.android.contacts/contacts");//尋找手機所有連絡人
Cursor cursor = contentResolver.query(uri, null, null, null, null);
startManagingCursor(cursor);//得到所有連絡人的遊標
//關鍵區段,使用繼承了SimpleCursorAdapter的自訂Adapter來實現連絡人資訊的展現,
//從而在每個連絡人名字後面添加一個checkbox選擇框
//R.layout.contactlistitem是ListiView中每個連絡人的布局xml,cursor為所有連絡人遊標資料
//String[]是所有要呈現在ListView中的資料列,
//int[]是String[]中的資料在contactlistitem中展現時對用的控制項id
//由於重寫了bindView方法把資料繫結在布局檔案上,所以展現的控制重點在bindView方法中
madapter=new MyAdapter(getApplicationContext(),R.layout.contactlistitem,cursor,
new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts.DISPLAY_NAME},
new int[]{R.id.contact_name,R.id.contact_name,R.id.multiple_checkbox});
listView.setAdapter(madapter);
//選擇最下方的全選時,切換所有checkBox的選中狀態
cbSelectAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
for(int i=0;i<madapter.getCount();i++) //讓adapter裡面的map全部為true;
{
madapter.map.put(i, true);
}
}
else
{
for(int i=0;i<madapter.getCount();i++)//讓adapter裡面的map全部為false;
{
madapter.map.put(i, false);
}
}
ListView listView = (ListView)findViewById(R.id.lvContact); //得到ListView;
listView.setAdapter(madapter);
}
});
// 為ListView添加單擊事件監聽器
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int position,
long id) {
CheckBox checkBox = (CheckBox) v.findViewById(R.id.multiple_checkbox);
checkBox.toggle();
if(checkBox.isChecked())
{
madapter.map.put(position, true);
}
else
{
madapter.map.put(position, false);
}
}
});
}
public class MyAdapter extends SimpleCursorAdapter
{
private LayoutInflater mInflater;
//定義一個HashMa存放每個連絡人的編號和一個bool值,布爾值用來判斷checkbox是否選中
HashMap<Integer, Boolean> map=new HashMap<Integer, Boolean>();
//建構函式迴圈遍曆HashMap,讓所有的連絡人預設都是出於非選中狀態
public MyAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
for(int i=0;i<c.getCount();i++)
{
map.put(i, false);
}
}
//使用bindView把資料繫結在自訂布局檔案中
public void bindView(View view, Context context, Cursor cursor) {
//得到每一個item的布局檔案
LinearLayout ll = null;
if (view == null) {
//使用contactlistitem.xml布局檔案
ll = (LinearLayout) mInflater.inflate(R.layout.contactlistitem,null);
} else {
ll = (LinearLayout)view;
}
//通過cursor得到每一個欄位的ColumnIndex
int idCol = cursor.getColumnIndex(ContactsContract.Contacts._ID);
//得到連絡人姓名
int nameCol = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int nCount = cursor.getPosition();
TextView txtName = (TextView)ll.findViewById(R.id.contact_name); //連絡人姓名展示控制項
String contactId=cursor.getString(idCol);//得到連絡人id;
txtName.setText(cursor.getString(nameCol)+" ");
final CheckBox ckSel = (CheckBox)ll.findViewById(R.id.multiple_checkbox);
//響應連絡人清單上的checkbox,點擊時切換選中行的checkBox狀態
ckSel.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
map.put(nCount, true);
}
else
{
map.put(nCount, false);
}
}
});
//根據HashMap中的當前行的狀態布爾值,設定該CheckBox是否選中
ckSel.setChecked(map.get(nCount));
}
}
}
4.修改AndroidManifest.xml添加讀取連絡人清單許可權
<uses-permission android:name ="android.permission.READ_CONTACTS"/>
啟動模擬器運行程式就可以看到效果了。