日期:2014-05-16 浏览次数:20558 次
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[] sourceSta