惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 生如夏花之灿烂

本人讲课时录制的Android应用开发技术教学视频 jsoup使用样式class抓取数据时空格的处理 我的第一款anroid软件作品--短信精灵1.0 Android入门教程(三十五)------在Android上使用ZXing识别条形码/二维码(转) - 生如夏花之灿烂 Android入门教程(三十四)------之多级树形菜单的实现 (转) Android入门教程(三十三)------之ListView自适应实现表格(转) Android入门教程(三十二)------之SQLite分页表格(转) Android入门教程(三十一)------SQLite分页读取(转) Android入门教程(三十)------之XML解析与生成(转) - 生如夏花之灿烂 Android入门教程(二十九)------之BroadcastReceiver (转) Android入门教程(二十八)------之Service(转) Android入门教程(二十七)------之Style与Theme (转) Android入门教程(二十六)------之ActivityGroup + GridView 实现Tab分页标签(转) Android入门教程(二十五)------之画图(转) Android入门教程(二十四)------之Gallery + ImageSwitcher(转) Android入门教程(二十三)------之Gallery(转) Android入门教程(二十二)------之TabHost,TabWidget(转) Android入门教程(二十一)------之PopupWindow (转) Android入门教程(二十)之--之AlertDialog(转)
(原创)Android入门教程(三十六)------实现手机联系人的全选
生如夏花之灿烂 · 2011-08-11 · via 博客园 - 生如夏花之灿烂

开发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"/>

启动模拟器运行程序就可以看到效果了。