Android Studio SQLite Database Example
Ref:http://instinctcoder.com/android-studio-sqlite-database-example/
By:
非常好的例程,按照步骤一步步做就可以。
毕竟更新日期又过了一年,有些部分需要稍微修改,详见后文。
测试环境:Android Studio 2.3.3 , Windows 10
【注意】
1. 如果工程名和域名与作者不一样,需要在自己的程序修改包名。例如我的是 package com.example.sqlitedb; 与作者不同,记得跟自己程序保持一致就可以了。
2. 其中的两个xml文件中,部分地方需要添加values变量,否则会报错。由于只是美观问题,简单起见,可以删掉红色的部分。简洁版如下:
activity_main.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add" android:id="@+id/btnAdd" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@android:id/list" android:layout_centerHorizontal="true" android:layout_alignParentTop="true" android:layout_above="@+id/btnAdd" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="List All" android:id="@+id/btnGetAll" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/btnAdd" /> </RelativeLayout>
activity_student_detail.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Name" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Email" android:id="@+id/textView2" android:layout_below="@+id/textView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="29dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Age" android:id="@+id/textView3" android:layout_below="@+id/textView2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="29dp" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:id="@+id/editTextName" android:layout_above="@+id/textView2" android:layout_toRightOf="@+id/textView" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:ems="10" android:id="@+id/editTextEmail" android:layout_above="@+id/textView3" android:layout_toRightOf="@+id/textView" android:layout_alignRight="@+id/editTextName" android:layout_alignEnd="@+id/editTextName" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="number" android:ems="10" android:id="@+id/editTextAge" android:layout_alignBottom="@+id/textView3" android:layout_alignLeft="@+id/editTextEmail" android:layout_alignStart="@+id/editTextEmail" android:layout_alignRight="@+id/editTextEmail" android:layout_alignEnd="@+id/editTextEmail" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save" android:id="@+id/btnSave" android:layout_alignParentBottom="true" android:layout_toLeftOf="@+id/btnClose" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Close" android:id="@+id/btnClose" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete" android:id="@+id/btnDelete" android:layout_alignTop="@+id/btnSave" android:layout_toLeftOf="@+id/btnSave" /> </RelativeLayout>
view_student_entry
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:id="@+id/student_Id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" /> <TextView android:id="@+id/student_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="6dip" android:paddingTop="6dip" android:textSize="22sp" android:textStyle="bold" /> </LinearLayout>
3. 新打开的活动Activity,记得在 AndroidManifest.xml中注册。作者文中没提醒,新手看不懂Debug,容易产生困惑。黄色背景处即为添加代码。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sqlitedb"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".StudentDetail"/> </application> </manifest>
4. 根据系统提示ActionBarActivity过时了,更换为AppCompatActivity即可。程序的其他部分从作者文章中直接粘贴即可,经过验证,正确无误。
5. 打开新的Activity略感延迟(模拟器上),不知为什么。
6. 感兴趣的朋友可以在这个基础上优化。
界面如下: