2022-11-19学习内容-Server端代码编写-Client端代码编写
1.Server端代码编写
1.1UserDBHelper.java
package com.example.chapter07_server.database; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class UserDBHelper extends SQLiteOpenHelper { private static final String DB_NAME = "user.db"; public static final String TABLE_NAME = "USER_INFO"; private static final int DB_VERSION = 1; private static UserDBHelper mHelper = null; private UserDBHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } // 利用单例模式获取数据库帮助器的唯一实例 public static UserDBHelper getInstance(Context context) { if (mHelper == null) { mHelper = new UserDBHelper(context); } return mHelper; } // 创建数据库,执行建表语句 @Override public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" + "_ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + " NAME VARCHAR NOT NULL," + " AGE INTEGER NOT NULL," + " HEIGHT LONG NOT NULL," + " WEIGHT FLOAT NOT NULL," + " MARRIED INTEGER NOT NULL);"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
1.2UserInfoProvider.java
package com.example.chapter07_server.provider; import android.content.ContentProvider; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.util.Log; import com.example.chapter07_server.database.UserDBHelper; public class UserInfoProvider extends ContentProvider { private UserDBHelper dbHelper; @Override public boolean onCreate() { Log.d("ning", "UserInfoProvider onCreate"); dbHelper = UserDBHelper.getInstance(getContext()); return true; } // content://com.example.chapter07_server.provider.UserInfoProvider/user @Override public Uri insert(Uri uri, ContentValues values) { Log.d("ning", "UserInfoProvider insert"); SQLiteDatabase db = dbHelper.getWritableDatabase(); db.insert(UserDBHelper.TABLE_NAME, null, values); return uri; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Log.d("ning", "UserInfoProvider query"); SQLiteDatabase db = dbHelper.getReadableDatabase(); return db.query(UserDBHelper.TABLE_NAME, projection, selection, selectionArgs, null, null, null); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { // Implement this to handle requests to delete one or more rows. throw new UnsupportedOperationException("Not yet implemented"); } @Override public String getType(Uri uri) { // TODO: Implement this to handle requests for the MIME type of the data // at the given URI. throw new UnsupportedOperationException("Not yet implemented"); } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO: Implement this to handle requests to update one or more rows. throw new UnsupportedOperationException("Not yet implemented"); } }
2.Client端代码编写
2.1activity_content_write.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="姓名:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_name" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:layout_weight="1" android:background="@drawable/editext_selector" android:hint="请输入姓名" android:inputType="text" android:maxLength="12" android:text="Jack" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_age" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="年龄:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_age" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:layout_weight="1" android:background="@drawable/editext_selector" android:hint="请输入年龄" android:inputType="number" android:maxLength="2" android:text="18" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_height" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="身高:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_height" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:layout_weight="1" android:background="@drawable/editext_selector" android:hint="请输入身高" android:inputType="number" android:maxLength="3" android:text="180" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_weight" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="体重:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_weight" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:layout_weight="1" android:background="@drawable/editext_selector" android:hint="请输入体重" android:inputType="numberDecimal" android:maxLength="5" android:text="180" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <CheckBox android:id="@+id/ck_married" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:gravity="center" android:text="已婚" android:textColor="@color/black" android:textSize="17sp" /> <Button android:id="@+id/btn_save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存" android:textColor="@color/black" android:textSize="17sp" /> <Button android:id="@+id/btn_read" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="读取" android:textColor="@color/black" android:textSize="17sp" /> <Button android:id="@+id/btn_delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout>