当前位置:数据库 > SQLite >>

Android[中级教程]第三章 数据存储之SQLite

这一章我们来学习Android数据库SQLite,还是接上一章的,对于唐僧师徙去西天,三个徙弟得要杀妖怪啊,那得有个汇总啊,有个记数啊,这里我们就用SQLite来存储各徙弟杀死妖怪的数量,OK,上main.xml

 

 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" android:layout_width="match_parent" 

    android:layout_height="match_parent"> 

    <TextView android:layout_height="wrap_content" 

        android:textAppearance="?android:attr/textAppearanceMedium" 

        android:layout_width="wrap_content" android:text="西游记各主人公杀死妖怪数" 

        android:id="@+id/textView1"></TextView> 

    <Button android:text="增加一个主角" android:id="@+id/add_btn" 

        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 

    <Button android:text="更新主角杀死妖怪数" android:id="@+id/update_btn" 

        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 

    <Button android:text="删除一个主角" android:id="@+id/del_btn" 

        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 

    <ListView android:layout_height="wrap_content" android:id="@+id/listView1" 

        android:layout_width="match_parent"></ListView> 

 

</LinearLayout> 

这里定义了三个按钮,一个ListView,接下来又定义了三个Layout,分别显示"增加一个主角","更新主角杀死妖怪数","删除一个主角"的页面,分别如下:

 

add_item.xml

 

 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" android:layout_width="match_parent" 

    android:layout_height="match_parent" android:padding="10dp"> 

    <TextView android:layout_height="wrap_content" 

        android:textAppearance="?android:attr/textAppearanceMedium" 

        android:layout_width="wrap_content" android:text="主角名称"></TextView> 

    <EditText android:layout_height="wrap_content" 

        android:layout_width="match_parent" android:id="@+id/add_name"> 

        <requestFocus></requestFocus> 

    </EditText> 

    <TextView android:layout_height="wrap_content" 

        android:textAppearance="?android:attr/textAppearanceMedium" 

        android:layout_width="wrap_content" android:text="杀死妖怪数量"></TextView> 

    <EditText android:layout_height="wrap_content" 

        android:layout_width="match_parent" android:id="@+id/add_number" 

        android:inputType="number"></EditText> 

 

</LinearLayout> 

update_item.xml

 

 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" android:layout_width="match_parent" 

    android:layout_height="match_parent" android:padding="10dp"> 

    <TextView android:layout_height="wrap_content" 

        android:textAppearance="?android:attr/textAppearanceMedium" 

        android:layout_width="wrap_content" android:text="ID"></TextView> 

    <EditText android:layout_height="wrap_content" 

        android:layout_width="match_parent" android:id="@+id/update_id" 

        android:inputType="number"> 

        <requestFocus></requestFocus> 

    </EditText> 

    <TextView android:layout_height="wrap_content" 

        android:textAppearance="?android:attr/textAppearanceMedium" 

        android:layout_width="wrap_content" android:text="杀死妖怪数量"></TextView> 

    <EditText android:layout_height="wrap_content" 

        android:layout_width="match_parent" android:id="@+id/update_number" 

        android:inputType="number"></EditText> 

 

</LinearLayout> 

delete_item.xml

 

 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" android:layout_width="match_parent" 

    android:layout_height="match_parent" android:padding="10dp"> 

    <TextView android:layout_height="wrap_content" 

        android:textAppearance="?android:attr/textAppearanceMedium" 

        android:layout_width="wrap_content" android:text="ID"></TextView> 

    <EditText android:layout_height="wrap_content" 

        android:layout_width="match_parent" android:id="@+id/delete_id" 

        android:inputType="number"> 

        <requestFocus></requestFocus> 

    </EditText> 

 

补充:移动开发 , Android ,
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,