一.需求
首先说一下需求:一个activity中需要添加联系人用listview展示,现在要点击添加联系人按钮要获取联系人的列表到一个activity中让用户去选择联系人然后回传到第一个activity中.
好吧,就这么一个简单的功能我却做了一天,承认自己菜...在做的途中查阅了一些资料.包括如何获取通讯录的列表以及如何将checkedbox选中的值筛选出来重新组成一个数组.
获取联系人列表类:
public class GetPhoneNumberFromMobile {
private List<NotifyParty> list;
public List<NotifyParty> getPhoneNumberFromMobile(Context context) {
// TODO Auto-generated constructor stub
list = new ArrayList<NotifyParty>();
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
//moveToNext方法返回的是一个boolean类型的数据
while (cursor.moveToNext()) {
//读取通讯录的姓名
String name = cursor.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//读取通讯录的号码
String number = cursor.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
NotifyParty phoneInfo = new NotifyParty(name, number);
list.add(phoneInfo);
}
return list;
}}
这个类中主要的方法就是创建游标,然后通过move.next方法将通讯录中的联系人全部读取出来,然后将姓名和电话两个字段加到之前定义好的List<NotifyParty>中去.
接下来是关于自定义的checkedbox类:
public class CheckableFrameLayout extends FrameLayout implements Checkable{
public CheckableFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
private boolean mChecked = false;
@Override
public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
refreshDrawableState();
for (int i = 0, len = getChildCount(); i < len; i++) {
View child = getChildAt(i);
if(child instanceof Checkable){
((Checkable) child).setChecked(checked);
}
}
}
}
@Override
public boolean isChecked() {
return mChecked;
}
@Override
public void toggle() {
setChecked(!mChecked);
}
}
listview中还要加一个属性 android:choiceMode="multipleChoice"加上这个属性是为了让listview支持多选,而且checkedbox是在listview中为了防止listview滑动的时候选中的状态会改变,要让界面被自定义的CheckableFrameLayout包裹着.
<?xml version="1.0" encoding="utf-8"?>
<net.lhsoft.safetymanager.view.CheckableFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="68dp">
<CheckBox
android:id="@+id/checkbox"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:button="@drawable/checkbox_style"
android:clickable="false"
android:focusable="false" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="17dp"
android:layout_marginTop="17dp"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
<TextView
android:id="@+id/phone"
android:textSize="14sp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</net.lhsoft.safetymanager.view.CheckableFrameLayout>
具体为什么这么做还请大牛指点!.
将选中的数据放到map中去:
在adapter中调用setOnCheckedChangeListener方法.
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
state.put(position,indexEntity);
}else {
state.remove(position);
}
}
});
viewHolder.checkbox.setChecked((state.get(position) == null ? false : true));
在ContactsActivity中传值
HashMap<Integer, NotifyParty> state =contactsAdapter.state; Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putSerializable("map", state); intent.putExtras(bundle); this.setResult(1,intent); finish();
在第一个activity中通过onActivityResult方法获取值加到原来的list中去
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);
//获取map
HashMap<Integer, NotifyParty> state = (HashMap<Integer, NotifyParty>) data.getSerializableExtra("map");
循环遍历map
for (Map.Entry<Integer,NotifyParty> entry:state.entrySet())
{
//entry.getValue()获取Value中的值
//将Value中的值添加到lists中去
lists.add(entry.getValue());
//刷新界面
adapter.notifyDataSetChanged();
}
该功能的基本代码和思路都到这里了,感谢博客大牛们提供的参考资料.