小功能----获取联系人,带CheckBox的listview,数据的获取list的重组
来源:互联网 发布:优分期网络连接错误 编辑:程序博客网 时间:2024/05/06 22:41
一.需求
首先说一下需求:一个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(); }
该功能的基本代码和思路都到这里了,感谢博客大牛们提供的参考资料.
0 0
- 小功能----获取联系人,带CheckBox的listview,数据的获取list的重组
- 带checkBox的ListView
- 带CheckBox的ListView
- 带CheckBox的ListView
- android 获取listview checkbox选择的值
- android 获取listview checkbox选择的值
- 如何获取listview里选中的checkbox
- android 获取listview checkbox选择的值
- 获取listview checkbox选择的值 android
- 获取手机中联系人的数据
- 获取系统的联系人
- 获取联系人的电话号码
- 获取手机的联系人
- 带checkbox的listview,全选、删除、增加单条功能
- 获取listview里的数据,
- bootstrapTable 获取选中CheckBox的所有数据
- Android 在ListView中使用CheckBox,实现增加联系人的功能
- 获取选中的checkbox
- LeetCode 11
- Springmvc入门小程序
- 《Python自然语言处理》学习笔记(三)
- 仿微信查找聊天记录自动搜索关键字
- javascript基础 一个简单小开关
- 小功能----获取联系人,带CheckBox的listview,数据的获取list的重组
- STM32简介
- C++基础学习之8 - 玩转DLL
- 【java新】Optional pk 空指针
- 第26课 Python文件的写操作
- C++学习笔记(零)
- Html5 meta标签用法总结
- vector原理及其成员函数使用总结
- C++基础学习之9 - MVC机制