zoukankan      html  css  js  c++  java
  • Android SQLite 学习5

    Android Studio SQLite Database Example

    Ref:http://instinctcoder.com/android-studio-sqlite-database-example/

    By: TAN WOON HOW · PUBLISHED APRIL 9, 2014 · UPDATED JUNE 23, 2016

    非常好的例程,按照步骤一步步做就可以。

    毕竟更新日期又过了一年,有些部分需要稍微修改,详见后文。

    测试环境:Android Studio 2.3.3 , Windows 10 

    【注意】

    1. 如果工程名和域名与作者不一样,需要在自己的程序修改包名。例如我的是 package com.example.sqlitedb; 与作者不同,记得跟自己程序保持一致就可以了。

    2. 其中的两个xml文件中,部分地方需要添加values变量,否则会报错。由于只是美观问题,简单起见,可以删掉红色的部分。简洁版如下:

    activity_main.xml

    <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>
    View Code

    activity_student_detail.xml

    <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 Code

    view_student_entry

    <?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>
    View Code

    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. 感兴趣的朋友可以在这个基础上优化。


    界面如下:

  • 相关阅读:
    k8s 组件介绍-kube-controller-manager
    k8s 组件介绍-API Server
    ELK+filebeat+redis 日志分析平台
    Logstash配置文件详情
    Logstash,Fluentd, Logtail对比伤害
    公司redis
    Linux之网络ping(unknown host)故障及yum no more mirrors to try
    Linux思维导图之计划任务
    Linux思维导图之进程管理
    Linux思维导图之网络管理
  • 原文地址:https://www.cnblogs.com/hbuwyg/p/7048857.html
Copyright © 2011-2022 走看看