日期:2014-05-16 浏览次数:20610 次
2.实现显示时默认选中项控制.
3.实现修改后保存,根据id保存.4.数据库处理使用AHibernate1.1,详见: http://blog.csdn.net/lk_blog/article/details/8201449
android使用Dialog实现复选功能与数据库结合综合实例见下篇博客: http://blog.csdn.net/lk_blog/article/details/8515403
本示例效果图:
主要代码:spinner.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="修改学生[李坤]所在班级" /> <Spinner android:id="@+id/sprClass" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnSave" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="保存"/> </LinearLayout>SpinnerActivity.java
package com.tgb.lk.demo; import java.util.List; import com.tgb.lk.demo.R; import com.tgb.lk.demo.dao.impl.ClassesDaoImpl; 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.content.Context; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.Toast; public class SpinnerActivity extends Activity { private Spinner sprClass; private Button btnSave; private ClassesDaoImpl clsDao; private StudentDaoImpl stuDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner); clsDao = (clsDao == null ? new ClassesDaoImpl(this) : clsDao); stuDao = (stuDao == null ? new StudentDaoImpl(this) : stuDao); sprClass = (Spinner) findViewById(R.id.sprClass); btnSave = (Button) findViewById(R.id.btnSave); btnSave.setOnClickListener(listener); List<CItem> items = clsDao.getCItemClasses(); sprClass.setAdapter(buildSpinnerAdpater(this, items)); sprClass.setPrompt("请选择"); // 这个演示示例修改的学号为1的学生的信息,初始化数据中学号1没有自动生成主键,实际项目中可以根据查询的结果来传入id. int classesId = stuDao.get(1).getClassesId(); setSpinnerInitId(sprClass, classesId, items);// 设置Spinner默认的选中项 } // 得到spinner的选中的Id public int getSpinnerSelectId(Spinner spinner) { CItem item = (CItem) spinner.getSelectedItem(); if (item != null) { return Integer.parseInt(item.getId()); } return 0; } // 设置Spinner打开时默认的选中项 public void setSpinnerInitId(Spinner spinner, int initId, List<CItem> items) { // 初始化默认选项 if (initId != 0) { String id = String.valueOf(initId); for (int i = 0; i < items.size(); i++) { i