本示例说明:
其他代码:
CItem.java:
DBHelper.java:
1.使用Dialog实现复选功能并与后台数据通过Id绑定.
2.实现显示时默认选中项控制.
3.实现修改后保存,根据id保存.
4.数据库处理使用AHibernate1.1,详见:http://blog.csdn.net/lk_blog/article/details/8201449
单选按钮实例见上篇博客:http://blog.csdn.net/lk_blog/article/details/8515347
本示例效果图:
主要代码:
dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="修改班级[五期提高班]内学生信息" />
<Button
android:id="@+id/btnModify"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="修改" />
</LinearLayout>
DialogActivity.java:
package com.tgb.lk.demo;
import java.util.List;
import com.tgb.lk.demo.R;
import com.tgb.lk.demo.dao.impl.StudentDaoImpl;
import com.tgb.lk.demo.model.Student;
import com.tgb.lk.demo.util.CItem;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class DialogActivity extends Activity {
private StudentDaoImpl stuDao;
private Button btnModify;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
btnModify = (Button) findViewById(R.id.btnModify);
btnModify.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnModify:
stuDao = (stuDao == null ? new StudentDaoImpl(DialogActivity.this) : stuDao);
//这个演示示例修改的班号为2的班级内学生,初始化数据中班号1没有自动生成主键,实际项目中可以根据查询的结果来传入id.
List<CItem> list = stuDao.getCItemStudents(2);
showDialog(list, 1);
break;
default:
break;
}
}
};
public void toastShow(String text) {
Toast.makeText(this, text, 1000).show();
}
//多选对话框
public void showDialog(List<CItem> list, final int classesId) {
int listSize = list.size();
String[] ids = new String[listSize];// 所有id集合
String[] values = new String[listSize]; // 所有name集合
boolean[] flags = new boolean[listSize]; // 定义对象是否与实体建立了关联关系.
// 初始化关联关系
for (int j = 0; j < listSize; j++) {
CItem item = list.get(j);
ids[j] = item.getId();
values[j] = item.getValue();
flags[j] = item.getFlag();
}
// 定义复选框选项
final String[] choiceItems = values;
// 复选框默认值:false=未选;true=选中
final boolean[] tempStatus = new boolean[flags.length];
System.arraycopy(flags, 0, tempStatus, 0, flags.length);// 此处必须深复制
final boolean[] sourceStatus = new boolean[flags.length];
System.arraycopy(flags, 0, sourceStatus, 0, flags.length);// 此处必须深复制
final List<CItem> tempList = list;
// 创建对话框
new AlertDialog.Builder(this).setTitle("请选择")// 设置对话框标题
.setMultiChoiceItems(choiceItems, tempStatus,
new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
// 来回重复选择取消,得相应去改变item对应的bool值,点击确定时,根据这个bool[],得到选择的内容
tempStatus[which] = isChecked;
}
}) // 设置对话框[肯定]按钮
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dealRel(tempList, sourceStatus, tempStatus, classesId);//更新数据
}
}).setNegativeButton("取消", null)// 设置对话框[否定]按钮
.show();
}
// 更新数据,判断原来的关系和现在的关系,如果发生变化则更新
public void dealRel(List<CItem> list, boolean[] sourceStatus,
boolean[] targetStatus, int classesId) {
stuDao = (stuDao == null ? new StudentDaoImpl(DialogActivity.this) : stuDao);
for (int i = 0; i < targetStatus.length; i++) {
if (sourceStatus[i] != targetStatus[i]) {
Student student = stuDao.get(Integer.parseInt(list.get(i).getId()));
if (targetStatus[i] == true) {
student.setClassesId(classesId);//如果选中了则设置新班号.
} else {
student.setClassesId(0);//demo中处理时把没选中班号修改为了0.
}
stuDao.update(student);//调用AHibernate中的方法更新.
}
}
toastShow("保存成功!");
}
}
其他代码:
ClassesDaoImpl.java:
package com.tgb.lk.demo.dao.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.content.Context;
import com.tgb.lk.ahibernate.dao.impl.BaseDaoImpl;
import com.tgb.lk.demo.model.Classes;
import com.tgb.lk.demo.util.CItem;
import com.tgb.lk.demo.util.DBHelper;
//本文数据库处理引用jar包AHibernate1.1处理.
//AHibernate的详细使用教程示例地址: http://blog.csdn.net/lk_blog/article/details/7455992
//AHibernate源码交流地址: http://blog.csdn.net/lk_blog/article/details/7456125
//AHibernate jar包下载及源代码下载地址: http://download.csdn.net/detail/lk_blog/4222048
public class ClassesDaoImpl extends BaseDaoImpl<Classes> {
public ClassesDaoImpl(Context context) {
super(new DBHelper(context),Classes.class);
}
public void deleteAll() {
super.execSql("delete from t_classes", null);
}
//查询所有班级保存到List<CItem>.
public List<CItem> getCItemClasses(){
String sql = "select _id,name from t_classes";
List<Map<String, String>> list = super.query2MapList(sql, null);
List<CItem> retList = new ArrayList<CItem>();
for (Map<String, String> map : list) {
CItem item= new CItem();
item.setId(map.get("_id"));
item.setValue(map.get("name"));
retList.add(item);
}
return retList;
}
}
CItem.java:
package com.tgb.lk.demo.util;
//处理单选和多选框的工具实体类
public class CItem implements Cloneable{
private String id = "";
private String value = "";
private boolean flag = false;//控制多选框默认是否选中状态.
public CItem() {
}
public CItem(String _ID, String _Value) {
id = _ID;
value = _Value;
}
@Override
public String toString() {
// 这个必须重写才能保证显示正常,因为适配器在显示数据的时候,如果传入适配器的对象不是字符串的情况下,直接就使用对象.toString()
return value;
}
public String getId() {
return id;
}
public String getValue() {
return value;
}
public void setId(String id) {
this.id = id;
}
public void setValue(String value) {
this.value = value;
}
public boolean getFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
DBHelper.java:
package com.tgb.lk.demo.util;
import com.tgb.lk.ahibernate.util.MyDBHelper;
import com.tgb.lk.demo.model.Classes;
import com.tgb.lk.demo.model.Student;
import android.content.Context;
public class DBHelper extends MyDBHelper {
private static final String DBNAME = "school.db";// 数据库名
private static final int DBVERSION = 1;
private static final Class<?>[] clazz = { Student.class, Classes.class };// 要初始化的表
public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION, clazz);
}
}
Classes.java:
package com.tgb.lk.demo.model;
import com.tgb.lk.ahibernate.annotation.Column;
import com.tgb.lk.ahibernate.annotation.Id;
import com.tgb.lk.ahibernate.annotation.Table;
@Table(name = "t_classes")
public class Classes {
@Id
@Column(name = "_id")
private int id;
@Column(name = "name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Classes [id=" + id + ", name=" + name + "]";
}
}
Student.java:
package com.tgb.lk.demo.model;
import com.tgb.lk.ahibernate.annotation.Column;
import com.tgb.lk.ahibernate.annotation.Id;
import com.tgb.lk.ahibernate.annotation.Table;
//自动生成的建表语句:
//CREATE TABLE t_student (id INTEGER primary key autoincrement, classes TEXT, teacher_id INTEGER, name TEXT(20))
@Table(name = "t_student")
public class Student {
@Id
@Column(name = "_id")
private int id; // 主键,int类型,数据库建表时此字段会设为自增长
@Column(name = "name", length = 20)
private String name; // 名字长度一般不会超过20个字符吧,length=20数据字段的长度是20
@Column(name = "classesid")
private int classesId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getClassesId() {
return classesId;
}
public void setClassesId(int classesId) {
this.classesId = classesId;
}
@Override
public String toString() {
return "Student [classesId=" + classesId + ", id=" + id + ", name="
+ name + "]";
}
}
源代码下载地址:http://download.csdn.net/detail/lk_blog/5005962
限于本人水平有限,很多地方写的并不完美,希望大家不吝赐教.如果觉得本文对您有帮助请顶支持一下,如果有不足之处欢迎留言交流,希望在和大家的交流中得到提高.