Android基础整合项目之节日群发助手(二)
Android基础整合项目(一) 之节日群发助手part 2
——转载请注明出处:coder-pig
本节引言:
在上一节中我们已经做出了我们群发助手的第一个界面以及完成了联系人的读取以及数据库的
录入了,在这一节中将要完成的工作是:
1)自定义我们的ListView的列表项,两个TextView + CheckBox;
2)使用SimpleCursorAdapter将数据显示到ListView上;
3)实现listview的全选与全不选
4)点击重置按钮后,将数据库中的state都重置为-1
好了,那么开始本节的内容吧!
正文:
1.创建第二个Activity的布局:
布局代码如下:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/RelativeLayout1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context="com.jay.example.festivalsmshelper.MainActivity" >
- <TextView
- android:id="@+id/TextView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:text="选择要发送的人" />
- <ListView
- android:id="@+id/listcontacts"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_alignParentLeft="true"
- android:layout_below="@+id/TextView1"
- android:layout_above="@+id/buttomlayout"
- android:layout_marginTop="10dp" >
- </ListView>
- <LinearLayout
- android:id="@+id/buttomlayout"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:orientation="horizontal" >
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/btnall"
- android:text="全选" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/btnreset"
- android:text="重置" />
- <Button
- android:id="@+id/btnnext"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="下一步" />
- <TextView
- android:layout_marginLeft="20dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="已选"
- android:textSize="15sp" />
- <TextView
- android:layout_marginLeft="20dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/textshow"
- android:textSize="15sp" />
- </LinearLayout>
- </RelativeLayout>
2.创建Listview的列表项布局文件:
其实就是三等分了布局,另外还隐藏了两个textview
代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:gravity="center_horizontal"
- >
- <TextView
- android:gravity="center"
- android:id="@+id/listid"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:visibility="gone"
- />
- <TextView
- android:gravity="center"
- android:id="@+id/listname"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- />
- <TextView
- android:gravity="center"
- android:id="@+id/listphone"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- />
- <TextView
- android:id="@+id/listhide"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:visibility="gone"
- />
- <CheckBox
- android:focusable="false"
- android:clickable="false"
- android:id="@+id/listchoice"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- />
- </LinearLayout>
3.使用SimpleCursorAdapter绑定数据库与ListView:
代码如下:
- private void getContacts()
- {
- GetContactsService gs = new GetContactsService(ChooseActivity.this);
- Cursor cursor = gs.query("select _id,pname,pnumber,pstate from contacts", null);
- //注意:使用SimpleCursorAdapter的话,这个_id是必不可少的!不然一直报错
- //所以在前面需要弄一个隐藏的组件来放_id
- simpleCursorAdapter = new SimpleCursorAdapter(ChooseActivity.this, R.layout.listitem,
- cursor, new String[]{"_id","pname","pnumber","pstate"},
- new int[]{R.id.listid,R.id.listname,R.id.listphone,R.id.listhide});
- list.setAdapter(simpleCursorAdapter);
- }
注意!!!!使用SimpleCursorAdapter的话,绑定的数据库表中一定要有_id这个字段,或者as _id;
而且在绑定时取出的数据必须包含这个_id项,否则的话会报以下错误!
java.lang.IllegalArgumentException: column '_id' does not exist
4.在OnCreate( )中调用该方法:
在Activity中调用3的getContacts( ),就可以看到我们的已经把数据库的数据显示到ListView上了:
5.点击listview列表项执行的操作:
①为listView设置setOnItemClickListener方法,即点击事件
②点击后要左什么呢?改变checkbox的选中状态,以及统计有多少个选中,然后显示出来
先定义一个用于统计当前选中的复选框数量:
- private int setShow()
- {
- int num = 0;
- for(int i = 0;i < list.getChildCount();i++)
- {
- LinearLayout layout = (LinearLayout) list.getChildAt(i);
- CheckBox cbx = (CheckBox) layout.findViewById(R.id.listchoice);
- if(cbx.isChecked())num++;
- }
- return num;
- }
接着实现点击后复选框选中,以及已选数目的改变:
- list.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- CheckBox cb = (CheckBox) view.findViewById(R.id.listchoice);
- TextView itemhide = (TextView) view.findViewById(R.id.listhide);
- TextView itemid = (TextView) view.findViewById(R.id.listid);
- int state = Integer.parseInt(itemhide.getText().toString());
- //利用这个变量来区分按钮是否选中
- state *= -1;
- itemhide.setText(state+"");
- if(state == 1)cb.setChecked(true);
- else cb.setChecked(false);
- textshow.setText(setShow()+"项");
- }
- });
完成上述代码后运行出现以下效果:
6.全选功能的实现:
就是选中全部的列表项,接着显示数目而已!
- btnall.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- allflag *= -1;
- if(allflag == -1)
- {
- for(int i = 0;i < list.getChildCount();i++)
- {
- LinearLayout layout = (LinearLayout) list.getChildAt(i);
- CheckBox cbx = (CheckBox) layout.findViewById(R.id.listchoice);
- cbx.setChecked(true);
- btnall.setText("全不选");
- textshow.setText(setShow()+"项");
- }
- }
- else if(allflag == 1)
- {
- for(int i = 0;i < list.getChildCount();i++)
- {
- LinearLayout layout = (LinearLayout) list.getChildAt(i);
- CheckBox cbx = (CheckBox) layout.findViewById(R.id.listchoice);
- cbx.setChecked(false);
- btnall.setText("全选");
- textshow.setText(setShow()+"项");
- }
- }
- }
- });
上述代码实现后的效果:
7.下一步按钮点击触发事件的重写:
在这个按钮事件要完成的工作:
①遍历listview,获取checkbox为选中状态的,将对应联系人id存储到集合中!
②将集合存放到Intent中,it.putIntegerArrayListExtra("ids", checkedId);
代码如下:
- btnnext.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- ArrayList<Integer> checkedId = new ArrayList<Integer>();
- for(int i = 0;i < list.getChildCount();i++)
- {
- LinearLayout layout = (LinearLayout) list.getChildAt(i);
- CheckBox cbx = (CheckBox) layout.findViewById(R.id.listchoice);
- TextView txtid = (TextView) layout.findViewById(R.id.listid);
- if(cbx.isChecked())
- checkedId.add(Integer.parseInt(txtid.getText().toString()));
- }
- //跳转到第三个页面,同时把数据存储到intent对象中
- Intent it = new Intent(ChooseActivity.this,ThridActivity.class);
- it.putIntegerArrayListExtra("ids", checkedId);
- startActivity(it);
- }
- });
恩呢,第二个界面就做好了!本节也到此结束了!
知识点总结:
好了,最后总结下这节中用到的知识点:
1)在LinearLayout中使用weight属性将水平或者竖直方向平分成多份!
2)使用visibility对组件进行隐藏:visible(可见),invisible(不可见,但还占据空间),gone(不可见,也不占空间)
3)SimpleCursorAdapter绑定数据库与ListView需要注意必须要有:_id这个字段或者某字段 as _id;
4)如何统计listview复选框为选中状态的列数,遍历listview!
5)使用intent的putIntegerArrayListExtra存储ArrayList<T>集合类型的数据,传递到另一Activity中!
Android基础整合项目之节日群发助手(二)相关推荐
- Android基础总结: Camera2详解之一 API学习
Camera2的API出来有些年头了,只是赶项目多次使用,没时间好好总结,年终了,正好结合google的官方Camera2demo 和开发中使用的情况,做个详细梳理,研究总结之后,才发现Camera2 ...
- Android基础——项目的文件结构(三)
Android基础--项目的文件结构(三) 代码源文件夹与资源文件夹 [注]此项目文件结构仅限于Android Studio下的Android项目!!! 在一个Android项目中,代码源文件夹有4个 ...
- Android Studio实现学生信息管理系统,基础入门项目
文章目录 一.需求分析 二.开发环境 三.详细设计 3.1 项目结构 3.2 数据库 3.3 登录和注册 3.4 增删改查 四.项目演示 五.项目源码 一.需求分析 该学生信息管理系统具有添加学生信息 ...
- Android基础知识【项目实训-实现二级导航“今日活动”及读取数据库】【5】
[该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材] [项目题目]:校园订餐App设计 ...
- 2022 最新 Android 基础教程,从开发入门到项目实战【b站动脑学院】学习笔记——第三章:简单控件
第 3 章 简单控件 本章介绍了App开发常见的几类简单控件的用法,主要包括:显示文字的文本视图.容纳视图的常用布局.响应点击的按钮控件.显示图片的图像视图等.然后结合本章所学的知识,演示了一个实战项 ...
- 2022 最新 Android 基础教程,从开发入门到项目实战【b站动脑学院】学习笔记——第六章:数据存储
第 6 章 数据存储 本章介绍Android 4种存储方式的用法,包括共享参数SharedPreferences.数据库SQLite.存储卡文 件.App的全局内存,另外介绍Android重要组件-应 ...
- 2022 最新 Android 基础教程,从开发入门到项目实战【b站动脑学院】学习笔记——第五章:中级控件
第 5 章 中级控件 本章介绍App开发常见的几类中级控件的用法,主要包括:如何定制几种简单的图形.如何使用几种选择按钮.如何高效地输入文本.如何利用对话框获取交互信息等,然后结合本章所学的知识,演示 ...
- Android基础——项目的文件结构(二)
Android基础--项目的文件结构(二) AndroidManifest.xml文件分析 [注]此项目文件结构仅限于Android Studio下的Android项目!!! 在一个Android项目 ...
- 重学Android基础系列篇(五):Android虚拟机指令
前言 本系列文章主要是汇总了一下大佬们的技术文章,属于Android基础部分,作为一名合格的安卓开发工程师,咱们肯定要熟练掌握java和android,本期就来说说这些~ [非商业用途,如有侵权,请告 ...
最新文章
- 美版iphone更新系统无服务器,大量美版iPhone“伪装”成国行出现在各大平台,遇到一定不要买...
- node.js 腾讯镜像站_使用腾讯云提供的针对Nuget包管理器的缓存加速服务
- [云炬python3玩转机器学习]6-3线性回归中的梯度下降法
- Jakarta EE 8状态
- 华硕主板无盘启动bios设置_legacy和UEFI启动是什么?电脑BIOS设置开启进入UEFI启动方法...
- 关于js返回上一页的实现方法
- redis 值字符串前面部分乱码_解决spring boot redis序列化key-value乱码
- C#datagidview创立数据库,实现读写数据视频
- 筛选出英语与计算机成绩之和,职称计算机和英语考试的成绩什么地方能够查到 – 手机爱问...
- 我的盖洛普“优势识别器”测试证书
- 【sublime】sublime添加浏览器预览快捷键
- 编码技巧——全局异常捕获统一的返回体业务异常
- pdcp层的作用_LTE协议栈总体架构、PDCP层及RLC层概述
- 沈航计算机复试刷人,考研复试刷人严重吗被刷机率大吗
- (Java) 实现打印菱形图案
- 目标检测1——SAR影像舰船数据集
- Smarty (一)
- java 分权分域什么意思_基于云平台的分权分域系统分析与设计
- (转)我所经历的大数据平台发展史(四):互联网时代 • 下篇
- 2022-2028年全球与中国医用级AC-DC电源行业竞争格局与投资战略研究