android使用sqlite3

效果图:

android使用sqlite3           android使用sqlite3              android使用sqlite3

 

 项目结构图:

android使用sqlite3

 

 

activity_main.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context=".MainActivity">
 9 
10     <ScrollView
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content">
13     </ScrollView>
14 
15     <ListView
16         android:id="@+id/listview"
17         android:layout_width="match_parent"
18         android:layout_height="400dp">
19 
20     </ListView>
21 
22     <Button
23         android:id="@+id/btn_addDocument"
24         android:layout_width="wrap_content"
25         android:layout_height="wrap_content"
26         android:text="添加记录"
27         android:layout_gravity="center" />
28 
29 </LinearLayout>

 

activity_add_document.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context=".AddDocument">
 9 
10     <EditText
11         android:id="@+id/add_time"
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content"
14         android:hint="输入时间"/>
15 
16     <EditText
17         android:id="@+id/add_height"
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content"
20         android:hint="输入身高"/>
21 
22     <EditText
23         android:id="@+id/add_weight"
24         android:layout_width="match_parent"
25         android:layout_height="wrap_content"
26         android:hint="输入体重"/>
27 
28     <EditText
29         android:id="@+id/add_blood_pressure"
30         android:layout_width="match_parent"
31         android:layout_height="wrap_content"
32         android:hint="输入血压"/>
33 
34     <EditText
35         android:id="@+id/add_temperature"
36         android:layout_width="match_parent"
37         android:layout_height="wrap_content"
38         android:hint="输入体温"/>
39 
40     <EditText
41         android:id="@+id/add_pulse"
42         android:layout_width="match_parent"
43         android:layout_height="wrap_content"
44         android:hint="输入脉搏"/>
45 
46     <EditText
47         android:id="@+id/add_breathe"
48         android:layout_width="match_parent"
49         android:layout_height="wrap_content"
50         android:hint="输入呼吸"/>
51 
52     <Button
53         android:id="@+id/btn_hd_add"
54         android:layout_width="wrap_content"
55         android:layout_height="wrap_content"
56         android:text="添加"
57         android:layout_gravity="center"/>
58 
59 </LinearLayout>

 

document.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6 
 7     <TextView
 8         android:id="@+id/time"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="时间:2019.19.39"/>
12 
13     <TextView
14         android:id="@+id/height"
15         android:layout_toRightOf="@+id/time"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_marginLeft="20dp"
19         android:text="身高:150 cm"/>
20 
21     <TextView
22         android:id="@+id/weight"
23         android:layout_toRightOf="@+id/height"
24         android:layout_width="wrap_content"
25         android:layout_height="wrap_content"
26         android:layout_marginLeft="20dp"
27         android:text="体重:50 kg"/>
28 
29     <TextView
30         android:id="@+id/blood_pressure"
31         android:layout_below="@+id/time"
32         android:layout_width="wrap_content"
33         android:layout_height="wrap_content"
34         android:text="血压:120/80 mmHg"/>
35 
36     <TextView
37         android:id="@+id/temperature"
38         android:layout_below="@+id/height"
39         android:layout_toRightOf="@+id/blood_pressure"
40         android:layout_width="wrap_content"
41         android:layout_height="wrap_content"
42         android:layout_marginLeft="20dp"
43         android:text="体温:38.9 摄氏度"/>
44 
45     <TextView
46         android:id="@+id/pulse"
47         android:layout_below="@+id/blood_pressure"
48         android:layout_width="wrap_content"
49         android:layout_height="wrap_content"
50         android:text="脉搏:100 次/分"/>
51 
52     <TextView
53         android:id="@+id/breathe"
54         android:layout_below="@id/blood_pressure"
55         android:layout_toRightOf="@+id/pulse"
56         android:layout_marginLeft="20dp"
57         android:layout_width="wrap_content"
58         android:layout_height="wrap_content"
59         android:text="呼吸:100 次/分"/>
60 
61 </RelativeLayout>

 

MainActivity:
 1 package com.mingrisoft.health_document_test;
 2 
 3 import android.content.Intent;
 4 import android.database.Cursor;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.ListView;
10 import android.widget.SimpleAdapter;
11 import android.widget.Toast;
12 
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Map;
16 
17 public class MainActivity extends AppCompatActivity {
18     private DBOpenHelper dbOpenHelper;
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24 
25         Button btn_addDocument = findViewById(R.id.btn_addDocument);
26         ListView listView = findViewById(R.id.listview);
27         dbOpenHelper = new DBOpenHelper(MainActivity.this,"db_document",null,1);
28 
29         Cursor cursor = dbOpenHelper.getReadableDatabase().query("tb_document",null,null, null, null,null,null);
30         ArrayList<Map<String, String>> resultList = new ArrayList<>();
31 
32         while (cursor.moveToNext()) {
33             Map<String, String> map = new HashMap<>();
34             map.put("time", cursor.getString(1));
35             map.put("height", cursor.getString(2));
36             map.put("weight", cursor.getString(3));
37             map.put("blood_pressure", cursor.getString(4));
38             map.put("temperature", cursor.getString(5));
39             map.put("pulse", cursor.getString(6));
40             map.put("breathe", cursor.getString(7));
41             resultList.add(map);
42         }
43 
44         if (resultList == null || resultList.size() == 0) {
45             Toast.makeText(MainActivity.this,"记录为空",Toast.LENGTH_SHORT).show();
46         } else {
47             SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, resultList,
48                     R.layout.document, new String[]{"time", "height", "weight", "blood_pressure",
49                     "temperature", "pulse", "breathe"},
50                     new int[] {R.id.time, R.id.height, R.id.weight, R.id.blood_pressure,
51                     R.id.temperature, R.id.pulse, R.id.breathe});
52             listView.setAdapter(simpleAdapter);
53         }
54 
55         btn_addDocument.setOnClickListener(new View.OnClickListener() {
56             @Override
57             public void onClick(View v) {
58                 Intent intent = new Intent(MainActivity.this, AddDocument.class);
59                 startActivity(intent);
60             }
61         });
62     }
63 }

 

DBOpenHelper:
 1 package com.mingrisoft.health_document_test;
 2 
 3 import android.content.Context;
 4 import android.database.sqlite.SQLiteDatabase;
 5 import android.database.sqlite.SQLiteOpenHelper;
 6 import android.util.Log;
 7 
 8 public class DBOpenHelper extends SQLiteOpenHelper {
 9     final String CREATE_DOCUMENT = "create table tb_document (_id integer primary key autoincrement," +
10             "time, height, weight, blood_pressure, temperature, pulse, breathe)";
11 
12     public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
13         super(context, name, null, version);
14     }
15 
16     @Override
17     public void onCreate(SQLiteDatabase db) {
18         db.execSQL(CREATE_DOCUMENT);
19     }
20 
21     @Override
22     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
23         Log.i("健康档案表","版本更新"+oldVersion+"-->"+newVersion);
24     }
25 }

 

AddDocument:
 1 package com.mingrisoft.health_document_test;
 2 
 3 import android.content.ContentValues;
 4 import android.database.Cursor;
 5 import android.database.sqlite.SQLiteDatabase;
 6 import android.support.v7.app.AppCompatActivity;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.EditText;
11 import android.widget.Toast;
12 
13 public class AddDocument extends AppCompatActivity {
14     private DBOpenHelper dbOpenHelper;
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_add_document);
20 
21         dbOpenHelper = new DBOpenHelper(AddDocument.this,"db_document",null,1);
22         final EditText et_add_time = findViewById(R.id.add_time);
23         final EditText et_add_height = findViewById(R.id.add_height);
24         final EditText et_add_weight = findViewById(R.id.add_weight);
25         final EditText et_add_blood_pressure = findViewById(R.id.add_blood_pressure);
26         final EditText et_add_temperature = findViewById(R.id.add_temperature);
27         final EditText et_add_pulse = findViewById(R.id.add_pulse);
28         final EditText et_add_breathe = findViewById(R.id.add_breathe);
29         Button btn_hd_add = findViewById(R.id.btn_hd_add);
30 
31         btn_hd_add.setOnClickListener(new View.OnClickListener() {
32             @Override
33             public void onClick(View v) {
34                 String time = et_add_time.getText().toString();
35                 String height = et_add_height.getText().toString();
36                 String weight = et_add_weight.getText().toString();
37                 String blood_pressure = et_add_blood_pressure.getText().toString();
38                 String temperature = et_add_temperature.getText().toString();
39                 String pulse = et_add_pulse.getText().toString();
40                 String breathe = et_add_breathe.getText().toString();
41 
42                 if (time.equals("")) {
43                     Toast.makeText(AddDocument.this,"时间不能为空",Toast.LENGTH_SHORT).show();
44                 } else {
45                     String key = et_add_time.getText().toString();
46                     Cursor cursor = dbOpenHelper.getReadableDatabase().query("tb_document",null,"time=?",new String[]{key}, null,null,null);
47 
48                     if (cursor.getCount() != 0) {
49                         Toast.makeText(AddDocument.this,"时间不能重复",Toast.LENGTH_SHORT).show();
50                     } else {
51                         insertData(dbOpenHelper.getReadableDatabase(), time, height, weight, blood_pressure, temperature,
52                                 pulse, breathe);
53                         Toast.makeText(AddDocument.this, "添加成功", Toast.LENGTH_SHORT).show();
54                     }
55                 }
56             }
57         });
58     }
59 
60     private void insertData(SQLiteDatabase sqLiteDatabase, String time, String height, String weight, String blood_pressure
61             , String temperature, String pulse, String breathe) {
62         ContentValues contentValues = new ContentValues();
63         contentValues.put("time", time);
64         contentValues.put("height", height);
65         contentValues.put("weight", weight);
66         contentValues.put("blood_pressure", blood_pressure);
67         contentValues.put("temperature", temperature);
68         contentValues.put("pulse", pulse);
69         contentValues.put("breathe", breathe);
70 
71         sqLiteDatabase.insert("tb_document",null,contentValues);
72     }
73 }

 

Manifests:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.mingrisoft.health_document_test">
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:roundIcon="@mipmap/ic_launcher_round"
10         android:supportsRtl="true"
11         android:theme="@style/AppTheme">
12         <activity android:name=".MainActivity">
13             <intent-filter>
14                 <action android:name="android.intent.action.MAIN" />
15 
16                 <category android:name="android.intent.category.LAUNCHER" />
17             </intent-filter>
18         </activity>
19         <activity android:name=".AddDocument"></activity>
20     </application>
21 
22 </manifest>

 

android使用sqlite3

上一篇:vue + axios 图片上传


下一篇:让微信小程序每次请求的时候不改变session_id的方法