Android基础整合项目(一) 之节日群发助手part 2

——转载请注明出处:coder-pig

本节引言:

在上一节中我们已经做出了我们群发助手的第一个界面以及完成了联系人的读取以及数据库的

录入了,在这一节中将要完成的工作是:

1)自定义我们的ListView的列表项,两个TextView + CheckBox;

2)使用SimpleCursorAdapter将数据显示到ListView上;

3)实现listview的全选与全不选

4)点击重置按钮后,将数据库中的state都重置为-1

好了,那么开始本节的内容吧!

正文:

1.创建第二个Activity的布局:

布局代码如下:

[html]view plaincopyprint?
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/RelativeLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context="com.jay.example.festivalsmshelper.MainActivity" >
  8. <TextView
  9. android:id="@+id/TextView1"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_alignParentLeft="true"
  13. android:layout_alignParentTop="true"
  14. android:text="选择要发送的人" />
  15. <ListView
  16. android:id="@+id/listcontacts"
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent"
  19. android:layout_alignParentLeft="true"
  20. android:layout_below="@+id/TextView1"
  21. android:layout_above="@+id/buttomlayout"
  22. android:layout_marginTop="10dp" >
  23. </ListView>
  24. <LinearLayout
  25. android:id="@+id/buttomlayout"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:layout_alignParentBottom="true"
  29. android:layout_alignParentLeft="true"
  30. android:orientation="horizontal" >
  31. <Button
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:id="@+id/btnall"
  35. android:text="全选" />
  36. <Button
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:id="@+id/btnreset"
  40. android:text="重置" />
  41. <Button
  42. android:id="@+id/btnnext"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="下一步" />
  46. <TextView
  47. android:layout_marginLeft="20dp"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:text="已选"
  51. android:textSize="15sp" />
  52. <TextView
  53. android:layout_marginLeft="20dp"
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:id="@+id/textshow"
  57. android:textSize="15sp" />
  58. </LinearLayout>
  59. </RelativeLayout>

2.创建Listview的列表项布局文件:

其实就是三等分了布局,另外还隐藏了两个textview

代码如下:

[html]view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal"
  6. android:gravity="center_horizontal"
  7. >
  8. <TextView
  9. android:gravity="center"
  10. android:id="@+id/listid"
  11. android:layout_width="0dp"
  12. android:layout_weight="1"
  13. android:layout_height="match_parent"
  14. android:visibility="gone"
  15. />
  16. <TextView
  17. android:gravity="center"
  18. android:id="@+id/listname"
  19. android:layout_width="0dp"
  20. android:layout_weight="1"
  21. android:layout_height="match_parent"
  22. />
  23. <TextView
  24. android:gravity="center"
  25. android:id="@+id/listphone"
  26. android:layout_width="0dp"
  27. android:layout_weight="1"
  28. android:layout_height="match_parent"
  29. />
  30. <TextView
  31. android:id="@+id/listhide"
  32. android:layout_width="wrap_content"
  33. android:layout_height="match_parent"
  34. android:visibility="gone"
  35. />
  36. <CheckBox
  37. android:focusable="false"
  38. android:clickable="false"
  39. android:id="@+id/listchoice"
  40. android:layout_width="0dp"
  41. android:layout_weight="1"
  42. android:layout_height="match_parent"
  43. />
  44. </LinearLayout>

3.使用SimpleCursorAdapter绑定数据库与ListView:

代码如下:

[java]view plaincopyprint?
  1. private void getContacts()
  2. {
  3. GetContactsService gs = new GetContactsService(ChooseActivity.this);
  4. Cursor cursor = gs.query("select _id,pname,pnumber,pstate from contacts", null);
  5. //注意:使用SimpleCursorAdapter的话,这个_id是必不可少的!不然一直报错
  6. //所以在前面需要弄一个隐藏的组件来放_id
  7. simpleCursorAdapter = new SimpleCursorAdapter(ChooseActivity.this, R.layout.listitem,
  8. cursor, new String[]{"_id","pname","pnumber","pstate"},
  9. new int[]{R.id.listid,R.id.listname,R.id.listphone,R.id.listhide});
  10. list.setAdapter(simpleCursorAdapter);
  11. }


注意!!!!使用SimpleCursorAdapter的话,绑定的数据库表中一定要有_id这个字段,或者as _id;

而且在绑定时取出的数据必须包含这个_id项,否则的话会报以下错误!

java.lang.IllegalArgumentException: column '_id' does not exist

4.在OnCreate( )中调用该方法:

在Activity中调用3的getContacts( ),就可以看到我们的已经把数据库的数据显示到ListView上了:

5.点击listview列表项执行的操作:

①为listView设置setOnItemClickListener方法,即点击事件

②点击后要左什么呢?改变checkbox的选中状态,以及统计有多少个选中,然后显示出来

先定义一个用于统计当前选中的复选框数量:

[java]view plaincopyprint?
  1. private int setShow()
  2. {
  3. int num = 0;
  4. for(int i = 0;i < list.getChildCount();i++)
  5. {
  6. LinearLayout layout = (LinearLayout) list.getChildAt(i);
  7. CheckBox cbx = (CheckBox) layout.findViewById(R.id.listchoice);
  8. if(cbx.isChecked())num++;
  9. }
  10. return num;
  11. }

接着实现点击后复选框选中,以及已选数目的改变:

[java]view plaincopyprint?
  1. list.setOnItemClickListener(new OnItemClickListener() {
  2. @Override
  3. public void onItemClick(AdapterView<?> parent, View view,
  4. int position, long id) {
  5. CheckBox cb = (CheckBox) view.findViewById(R.id.listchoice);
  6. TextView itemhide = (TextView) view.findViewById(R.id.listhide);
  7. TextView itemid = (TextView) view.findViewById(R.id.listid);
  8. int state = Integer.parseInt(itemhide.getText().toString());
  9. //利用这个变量来区分按钮是否选中
  10. state *= -1;
  11. itemhide.setText(state+"");
  12. if(state == 1)cb.setChecked(true);
  13. else cb.setChecked(false);
  14. textshow.setText(setShow()+"项");
  15. }
  16. });

完成上述代码后运行出现以下效果:

6.全选功能的实现:

就是选中全部的列表项,接着显示数目而已!

[java]view plaincopyprint?
  1. btnall.setOnClickListener(new OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. allflag *= -1;
  5. if(allflag == -1)
  6. {
  7. for(int i = 0;i < list.getChildCount();i++)
  8. {
  9. LinearLayout layout = (LinearLayout) list.getChildAt(i);
  10. CheckBox cbx = (CheckBox) layout.findViewById(R.id.listchoice);
  11. cbx.setChecked(true);
  12. btnall.setText("全不选");
  13. textshow.setText(setShow()+"项");
  14. }
  15. }
  16. else if(allflag == 1)
  17. {
  18. for(int i = 0;i < list.getChildCount();i++)
  19. {
  20. LinearLayout layout = (LinearLayout) list.getChildAt(i);
  21. CheckBox cbx = (CheckBox) layout.findViewById(R.id.listchoice);
  22. cbx.setChecked(false);
  23. btnall.setText("全选");
  24. textshow.setText(setShow()+"项");
  25. }
  26. }
  27. }
  28. });

上述代码实现后的效果:

7.下一步按钮点击触发事件的重写:

在这个按钮事件要完成的工作:

①遍历listview,获取checkbox为选中状态的,将对应联系人id存储到集合中!

②将集合存放到Intent中,it.putIntegerArrayListExtra("ids", checkedId);

代码如下:

[java]view plaincopyprint?
  1. btnnext.setOnClickListener(new OnClickListener() {
  2. public void onClick(View v) {
  3. ArrayList<Integer> checkedId = new ArrayList<Integer>();
  4. for(int i = 0;i < list.getChildCount();i++)
  5. {
  6. LinearLayout layout = (LinearLayout) list.getChildAt(i);
  7. CheckBox cbx = (CheckBox) layout.findViewById(R.id.listchoice);
  8. TextView txtid = (TextView) layout.findViewById(R.id.listid);
  9. if(cbx.isChecked())
  10. checkedId.add(Integer.parseInt(txtid.getText().toString()));
  11. }
  12. //跳转到第三个页面,同时把数据存储到intent对象中
  13. Intent it = new Intent(ChooseActivity.this,ThridActivity.class);
  14. it.putIntegerArrayListExtra("ids", checkedId);
  15. startActivity(it);
  16. }
  17. });

恩呢,第二个界面就做好了!本节也到此结束了!

知识点总结:

好了,最后总结下这节中用到的知识点:

1)在LinearLayout中使用weight属性将水平或者竖直方向平分成多份!

2)使用visibility对组件进行隐藏:visible(可见),invisible(不可见,但还占据空间),gone(不可见,也不占空间)

3)SimpleCursorAdapter绑定数据库与ListView需要注意必须要有:_id这个字段或者某字段 as _id;

4)如何统计listview复选框为选中状态的列数,遍历listview!

5)使用intent的putIntegerArrayListExtra存储ArrayList<T>集合类型的数据,传递到另一Activity中!

Android基础整合项目之节日群发助手(二)相关推荐

  1. Android基础总结: Camera2详解之一 API学习

    Camera2的API出来有些年头了,只是赶项目多次使用,没时间好好总结,年终了,正好结合google的官方Camera2demo 和开发中使用的情况,做个详细梳理,研究总结之后,才发现Camera2 ...

  2. Android基础——项目的文件结构(三)

    Android基础--项目的文件结构(三) 代码源文件夹与资源文件夹 [注]此项目文件结构仅限于Android Studio下的Android项目!!! 在一个Android项目中,代码源文件夹有4个 ...

  3. Android Studio实现学生信息管理系统,基础入门项目

    文章目录 一.需求分析 二.开发环境 三.详细设计 3.1 项目结构 3.2 数据库 3.3 登录和注册 3.4 增删改查 四.项目演示 五.项目源码 一.需求分析 该学生信息管理系统具有添加学生信息 ...

  4. Android基础知识【项目实训-实现二级导航“今日活动”及读取数据库】【5】

    [该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材] [项目题目]:校园订餐App设计 ...

  5. 2022 最新 Android 基础教程,从开发入门到项目实战【b站动脑学院】学习笔记——第三章:简单控件

    第 3 章 简单控件 本章介绍了App开发常见的几类简单控件的用法,主要包括:显示文字的文本视图.容纳视图的常用布局.响应点击的按钮控件.显示图片的图像视图等.然后结合本章所学的知识,演示了一个实战项 ...

  6. 2022 最新 Android 基础教程,从开发入门到项目实战【b站动脑学院】学习笔记——第六章:数据存储

    第 6 章 数据存储 本章介绍Android 4种存储方式的用法,包括共享参数SharedPreferences.数据库SQLite.存储卡文 件.App的全局内存,另外介绍Android重要组件-应 ...

  7. 2022 最新 Android 基础教程,从开发入门到项目实战【b站动脑学院】学习笔记——第五章:中级控件

    第 5 章 中级控件 本章介绍App开发常见的几类中级控件的用法,主要包括:如何定制几种简单的图形.如何使用几种选择按钮.如何高效地输入文本.如何利用对话框获取交互信息等,然后结合本章所学的知识,演示 ...

  8. Android基础——项目的文件结构(二)

    Android基础--项目的文件结构(二) AndroidManifest.xml文件分析 [注]此项目文件结构仅限于Android Studio下的Android项目!!! 在一个Android项目 ...

  9. 重学Android基础系列篇(五):Android虚拟机指令

    前言 本系列文章主要是汇总了一下大佬们的技术文章,属于Android基础部分,作为一名合格的安卓开发工程师,咱们肯定要熟练掌握java和android,本期就来说说这些~ [非商业用途,如有侵权,请告 ...

最新文章

  1. 美版iphone更新系统无服务器,大量美版iPhone“伪装”成国行出现在各大平台,遇到一定不要买...
  2. node.js 腾讯镜像站_使用腾讯云提供的针对Nuget包管理器的缓存加速服务
  3. [云炬python3玩转机器学习]6-3线性回归中的梯度下降法
  4. Jakarta EE 8状态
  5. 华硕主板无盘启动bios设置_legacy和UEFI启动是什么?电脑BIOS设置开启进入UEFI启动方法...
  6. 关于js返回上一页的实现方法
  7. redis 值字符串前面部分乱码_解决spring boot redis序列化key-value乱码
  8. C#datagidview创立数据库,实现读写数据视频
  9. 筛选出英语与计算机成绩之和,职称计算机和英语考试的成绩什么地方能够查到 – 手机爱问...
  10. 我的盖洛普“优势识别器”测试证书
  11. 【sublime】sublime添加浏览器预览快捷键
  12. 编码技巧——全局异常捕获统一的返回体业务异常
  13. pdcp层的作用_LTE协议栈总体架构、PDCP层及RLC层概述
  14. 沈航计算机复试刷人,考研复试刷人严重吗被刷机率大吗
  15. (Java) 实现打印菱形图案
  16. 目标检测1——SAR影像舰船数据集
  17. Smarty (一)
  18. java 分权分域什么意思_基于云平台的分权分域系统分析与设计
  19. (转)我所经历的大数据平台发展史(四):互联网时代 • 下篇
  20. 2022-2028年全球与中国医用级AC-DC电源行业竞争格局与投资战略研究

热门文章

  1. 2020年CISP每日一练
  2. OS X El Capitan 10.11开启性能模式
  3. DB DBMS SQL 分别是什么?
  4. 辛普森悖论如何影响AB测试
  5. SQL Server查询IP地址等信息
  6. mysql 分数相加_分数求和
  7. CommaFeed:仿Google Readerd的RSS阅读器
  8. Mysql经典面试题(建议收藏)
  9. 【深度学习】详解 MAE
  10. Linux网络编程——千峰物联网笔记