前两天布置下了一个期末练习,其中的重点是两个表之间的SQLite关联操作。

拿到题目,首先需要建库和关联表,下面是代码。

我使用简单的表插入,将数据的提交卸载onCreate方法中,这样不会发生写在主线程中由于程序多次运行造成数据的多次创建。

当然写在MainActivity中也有避免多次运行的办法--通过SharedPreferences 的preferences.getBoolean设置boolean值,首次运行默认为true,在之后就全部置为false。

需要注意的是SharedPreferences本身不支持编辑,也就是不支持修改boolean isFirstRun = preferences.getBoolean("run", true);

中的值,需要调用editor启用编辑。

SharedPreferences preferences = this.getSharedPreferences("share", MODE_PRIVATE);

boolean isFirstRun = preferences.getBoolean("run", true);

SharedPreferences.Editor editor = preferences.edit();

if (isFirstRun) {

insert();

editor.putBoolean("run", false);

editor.commit();

} else {

editor.putBoolean("run", false);

editor.commit();

}

表中数据的关联需要使用映射关系。所以我们创建两个实体classInfo和stuInfo。在这个例子中因为stuInfo是外键,所以我们在stuInfo实体中创建一个classInfo

public class ClassInfo {
private int id;
private String className;

public class StuInfo {
private int id;
private int cID;
private String sName;
private String sSex;
private ClassInfo classInfo;

这只是两表关联的例子,多表之间关联的,我们也可以参照这个创建实体,创建实体后,我们就可以通过stuInfo.getClassInfo.getclassName()方法获取关联外键的字段。多表同理。

接下来写接口,这里我们只做查询到listView和增加功能作为示例。

public interface InfoDAO {
public List<StuInfo> getAllInfos();
public void addStuInfo(StuInfo stuInfo);
public List<ClassInfo> getAllClass();
}

接下来写查询方法

public List<StuInfo> getAllInfos() {
List<StuInfo> list = new ArrayList<StuInfo>();
SQLiteDatabase sdb = dbHelper.getReadableDatabase();
String sql = "Select * from stuInfo s,classInfo c Where s.id=c.id";
Cursor cursor = sdb.rawQuery(sql, null);
if(cursor!=null){
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("id"));
String sName = cursor.getString(cursor.getColumnIndex("sName"));
String sSex = cursor.getString(cursor.getColumnIndex("sSex"));
String cName = cursor.getString(cursor.getColumnIndex("className"));
ClassInfo classInfo = new ClassInfo(cName);
StuInfo stuInfo = new StuInfo(id, sName, sSex, classInfo);
list.add(stuInfo);
}
cursor.close();
}
sdb.close();
return list;
}

注意看我们StuInfo类中传入的classInfo是怎么获取的(通过new classInfo实体传入关联查询得到的cName,需要生成单参数构造方法)。

通过listView显示数据。注意cName是怎么获取的。

listView = (ListView) findViewById(R.id.listView1);
stuList = dao.getAllInfos();
data = new ArrayList<Map<String, Object>>();
for (Iterator iterator = stuList.iterator(); iterator.hasNext();) {
StuInfo stuInfo = (StuInfo) iterator.next();
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", stuInfo.getId());
map.put("sName", stuInfo.getsName());
map.put("cName", stuInfo.getClassInfo().getClassName());
map.put("sSex", stuInfo.getsSex());
data.add(map);
}
simpleAdapter = new SimpleAdapter(this, data, R.layout.list_item,
new String[] { "id", "sName", "cName", "sSex" }, new int[] {
R.id.textView1, R.id.textView2, R.id.textView3,
R.id.textView4 });
listView.setAdapter(simpleAdapter);

增加页面效果。

因为spinner的数据是通过sqlite服务器获取的,所以我们需要classInfo表中的数据

final RadioButton rab2 = (RadioButton) view.findViewById(R.id.radio2);
final Spinner spinner = (Spinner) view.findViewById(R.id.spinner1);
claList = dao.getAllClass();//获取classInfo的数据赋值给数组
claList.add(0,new ClassInfo("--请选择班级--"));//首项提示
ArrayAdapter<ClassInfo> arrayAdapter = new ArrayAdapter<ClassInfo>(
this, android.R.layout.simple_spinner_dropdown_item, claList);
spinner.setAdapter(arrayAdapter);

这是一个很普通的查询方法。

之后通过arrayAdapter将它设值给spinner

之后我们还可以看到,弹出来的dialog是一个自定义的对话框

LayoutInflater inflater = getLayoutInflater().from(this);

View view = inflater.inflate(R.layout.add_item, null);

builder.setView(view);

builder.setNegativeButton("取消",
null);//注册

builder.setPositiveButton("确定", null);//注册

builder.setView(view);

final AlertDialog dialog = builder.create();

dialog.show();

dialog.getButton(Dialog.BUTTON_POSITIVE).setOnClickListener(

      newOnClickListener() {



         @Override

         public voidonClick(View v) {

            StuInfo stuInfo =
null;

            ClassInfo classInfo =newClassInfo();

            //dao.getAllClass();

            intcID = classInfo.getId();

            String name =edName.getText().toString().trim();

            String rab =rab1.isChecked() ?"男":"女";



            stuInfo =newStuInfo(0,cID,name,rab);

            dao.addStuInfo(stuInfo);

            if (name ==null) {

               Toast.makeText(StuListActivity.this,"姓名不能为空!",

                     1000).show();

            }

            if(cID == -1) {

               Toast.makeText(StuListActivity.this,"传入值不能为空!",

                     1000).show();

            }

            dialog.dismiss();//取消显示

            showInfo();

         }

      });

dialog.getButton(Dialog.BUTTON_NEGATIVE).setOnClickListener(

      newOnClickListener() {



         @Override

         public voidonClick(View v) {

            //TODO Auto-generated method stub

            
dialog.dismiss();



         }

      });

附上源码:http://download.csdn.net/detail/jacxuan/9557633

android-SQLite数据库MVC关联实例源码(三层架构)的更多相关文章

  1. 45个android实例源码

    分享45个android实例源码,很好很强大http://www.apkbus.com/android-20978-1-1.html andriod闹钟源代码http://www.apkbus.com ...

  2. 分享45个android实例源码,很好很强大

    分享45个android实例源码,很好很强大 http://www.apkbus.com/android-20978-1-1.html 分享45个android实例源码,很好很强大http://www ...

  3. 分享45个android实例源码,很好很强大.收藏吧!!!

    andriod闹钟源代码 http://www.apkbus.com/android-20974-1-1.html android源码分享之指南针程序 http://www.apkbus.com/an ...

  4. 45个android实例源码分享

    分享45个android实例源码,很好很强大 http://www.apkbus.com/android-20978-1-1.html andriod闹钟源代码 http://www.apkbus.c ...

  5. 实例源码--Android日历实例源码

      下载源码   技术要点: 1.Android基础控件的使用 2.Android应用开发基础框架 3.源码带有非常详细的中文 注释 ...... 详细介绍: 1. Android应用开 发技术 此套 ...

  6. Android基础之在Eclipes中关联SDK源码和查看SDK源码

    在进行Android应用开发的时候,我们有时候需要查看某个类或接口的源码从而了解如何去使用一个类或者实现一个接口,查看源码有助于我们的学习某个封装的类的底层是如何实现的,这样可以帮助我们掌握类或者接口 ...

  7. MySQL数据库企业级应用实践(多实例源码编译)

    MySQL数据库企业级应用实践(多实例源码编译) 链接:https://pan.baidu.com/s/1ANGg3Kd_28BzQrA5ya17fQ 提取码:ekpy 复制这段内容后打开百度网盘手机 ...

  8. 实例源码--Android时钟源码

      下载源码   技术要点: 1.Android自定义时钟控件 2.源码带有非常详细的中文注释 ...... 详细介绍: 1.Android自定义时钟控件 通过继承视图继承类View进行自定义控制,实 ...

  9. JAVA上百实例源码以及开源项目

    简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级.中级.高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情.执着,对IT的憧憬. ...

随机推荐

  1. bzoj2441 [中山市选2011]小W的问题(debug中)

    2441: [中山市选2011]小W的问题 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 487  Solved: 186[Submit][Statu ...

  2. javaScript构造函数、原型、面向对象编程

    js最重要也是最核心的东西就是对象了,入行这么长时间,一直对面向对象一知半解.网上有很多介绍对象对象的内容,这里也做了很多借鉴, 尤其是阮一峰老师的文章.我这里写的大多例子都是阮一峰老师文章的例子,但 ...

  3. Spring 中解析 URL参数的几种方式

    1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @pa ...

  4. 百度之星复赛T5&&hdu6148

    Problem Description 众所周知,度度熊非常喜欢数字. 它最近发明了一种新的数字:Valley Number,像山谷一样的数字. 当一个数字,从左到右依次看过去数字没有出现先递增接着递 ...

  5. 寻找已排序的连个数组的第k个元素

    A,B是两个已经从小到大排序好了的数组,球这两个数组合并后的第k个元素. 很简单的想法,根据定义,把两个数组合并到一起,然后排序,然后就能得到了. 但是这样的复杂度是nlogn 还有就是用归并的思想, ...

  6. wchar_t类型的几个函数

    wchar_t是C/C++的字符数据类型,是一种扩展的字符存储方式. 在Windows下,wchar_t占2个字节(byte):在Linux下,wchar_t占4个字节 wchar_t类型主要用在国际 ...

  7. Flask-宏的相关知识。

    转自ITOYO:XIAOJINGJING Jinja2的宏功能有些类似于传统程序语言中的函数,既然是函数就有其声明和调用两个部分. 首先声明一个宏: <html lang="en&qu ...

  8. VirtualBox虚拟机安装CentOS 7

    新建虚拟机 因为比较简单,所以对于VirtualBox就不做过多介绍了,直接下载安装即可,安装好之后打开Oracle VM VirtualBox管理器,点击新建,选择Red Hat(根据windows ...

  9. 在Laravel中使用Middleware进行身份验证

    新建一个中间件: 方法写在handle中 判断用户是否登录而且是否是管理员,不是的话返回到主页 新建判断是否为管理员的方法 在kernel定义一个中间件,key是admin 注册群组路由:prefix ...

  10. [BZOJ1821][JSOI2010]Group 部落划分 Group 最小生成树 贪心

    1821: [JSOI2010]Group 部落划分 Group Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2943  Solved: 1390[S ...