• 基础复习——内容共享——通过ContentProvider封装数据——通过ContentResolver访问数据...


    利用ContentProvider只实现服务端App的数据封装,如果客户端App想访问对方的内部数据,就要通过内容解析器ContentResolver访问。

    内容解析器是客户端App操作服务端数据的工具,相对应的内容提供器是服务端的数据接口

    ContentResolver提供的方法与ContentProvider是一一对应的,比如query、insert、update、delete、getType等方法

    ==================================================================================================

    0、前提,用户信息表信息:  UserInfo

    1. package com.example.myapplication.bean;
    2. public class UserInfo {
    3. public long rowid; // 行号
    4. public int xuhao; // 序号
    5. public String name; // 姓名
    6. public int age; // 年龄
    7. public long height; // 身高
    8. public float weight; // 体重
    9. public boolean married; // 婚否
    10. public String update_time; // 更新时间
    11. public String phone; // 手机号
    12. public String password; // 密码
    13. public UserInfo() {
    14. rowid = 0L;
    15. xuhao = 0;
    16. name = "";
    17. age = 0;
    18. height = 0L;
    19. weight = 0.0f;
    20. married = false;
    21. update_time = "";
    22. phone = "";
    23. password = "";
    24. }
    25. }

    1、编写用户信息表的数据库帮助器:  UserDBHelper

    1. package com.example.myapplication.database;
    2. import android.annotation.SuppressLint;
    3. import android.content.ContentValues;
    4. import android.content.Context;
    5. import android.database.Cursor;
    6. import android.database.sqlite.SQLiteDatabase;
    7. import android.database.sqlite.SQLiteOpenHelper;
    8. import android.util.Log;
    9. import com.example.myapplication.bean.UserInfo;
    10. import java.util.ArrayList;
    11. import java.util.List;
    12. @SuppressLint("DefaultLocale")
    13. public class UserDBHelper extends SQLiteOpenHelper
    14. {
    15. private static final String TAG = "UserDBHelper";
    16. private static final String DB_NAME = "user.db"; // 数据库的名称
    17. private static final int DB_VERSION = 1; // 数据库的版本号
    18. private static UserDBHelper mHelper = null; // 数据库帮助器的实例
    19. private SQLiteDatabase mDB = null; // 数据库的实例
    20. public static final String TABLE_NAME = "user_info"; // 表的名称
    21. private UserDBHelper(Context context)
    22. {
    23. super(context, DB_NAME, null, DB_VERSION);
    24. }
    25. private UserDBHelper(Context context, int version)
    26. {
    27. super(context, DB_NAME, null, version);
    28. }
    29. // 利用单例模式获取数据库帮助器的唯一实例
    30. public static UserDBHelper getInstance(Context context, int version)
    31. {
    32. if (version > 0 && mHelper == null)
    33. {
    34. mHelper = new UserDBHelper(context, version);
    35. }
    36. else if (mHelper == null)
    37. {
    38. mHelper = new UserDBHelper(context);
    39. }
    40. return mHelper;
    41. }
    42. // 打开数据库的读连接
    43. public SQLiteDatabase openReadLink()
    44. {
    45. if (mDB == null || !mDB.isOpen())
    46. {
    47. mDB = mHelper.getReadableDatabase();
    48. }
    49. return mDB;
    50. }
    51. // 打开数据库的写连接
    52. public SQLiteDatabase openWriteLink()
    53. {
    54. if (mDB == null || !mDB.isOpen())
    55. {
    56. mDB = mHelper.getWritableDatabase();
    57. }
    58. return mDB;
    59. }
    60. // 关闭数据库连接
    61. public void closeLink()
    62. {
    63. if (mDB != null && mDB.isOpen())
    64. {
    65. mDB.close();
    66. mDB = null;
    67. }
    68. }
    69. // 创建数据库,执行建表语句
    70. public void onCreate(SQLiteDatabase db)
    71. {
    72. Log.d(TAG, "onCreate");
    73. String drop_sql = "DROP TABLE IF EXISTS " + TABLE_NAME + ";";
    74. Log.d(TAG, "drop_sql:" + drop_sql);
    75. db.execSQL(drop_sql);
    76. String create_sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
    77. + "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
    78. + "name VARCHAR NOT NULL," + "age INTEGER NOT NULL,"
    79. + "height LONG NOT NULL," + "weight FLOAT NOT NULL,"
    80. + "married INTEGER NOT NULL," + "update_time VARCHAR NOT NULL"
    81. //演示数据库升级时要先把下面这行注释
    82. + ",phone VARCHAR" + ",password VARCHAR"
    83. + ");";
    84. Log.d(TAG, "create_sql:" + create_sql);
    85. db.execSQL(create_sql); // 执行完整的SQL语句
    86. }
    87. // 修改数据库,执行表结构变更语句
    88. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    89. {
    90. Log.d(TAG, "onUpgrade oldVersion=" + oldVersion + ", newVersion=" + newVersion);
    91. if (newVersion > 1)
    92. {
    93. //Android的ALTER命令不支持一次添加多列,只能分多次添加
    94. String alter_sql = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + "phone VARCHAR;";
    95. Log.d(TAG, "alter_sql:" + alter_sql);
    96. db.execSQL(alter_sql);
    97. alter_sql = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + "password VARCHAR;";
    98. Log.d(TAG, "alter_sql:" + alter_sql);
    99. db.execSQL(alter_sql);
    100. }
    101. }
    102. // 根据指定条件删除表记录
    103. public int delete(String condition)
    104. {
    105. // 执行删除记录动作,该语句返回删除记录的数目
    106. return mDB.delete(TABLE_NAME, condition, null);
    107. }
    108. // 删除该表的所有记录
    109. public int deleteAll()
    110. {
    111. // 执行删除记录动作,该语句返回删除记录的数目
    112. return mDB.delete(TABLE_NAME, "1=1", null);
    113. }
    114. // 往该表添加一条记录
    115. public long insert(UserInfo info)
    116. {
    117. List<UserInfo> infoList = new ArrayList<UserInfo>();
    118. infoList.add(info);
    119. return insert(infoList);
    120. }
    121. // 往该表添加多条记录
    122. public long insert(List<UserInfo> infoList)
    123. {
    124. long result = -1;
    125. for (int i = 0; i < infoList.size(); i++)
    126. {
    127. UserInfo info = infoList.get(i);
    128. List<UserInfo> tempList = new ArrayList<UserInfo>();
    129. // 如果存在同名记录,则更新记录
    130. // 注意条件语句的等号后面要用单引号括起来
    131. if (info.name != null && info.name.length() > 0)
    132. {
    133. String condition = String.format("name='%s'", info.name);
    134. tempList = query(condition);
    135. if (tempList.size() > 0)
    136. {
    137. update(info, condition);
    138. result = tempList.get(0).rowid;
    139. continue;
    140. }
    141. }
    142. // 如果存在同样的手机号码,则更新记录
    143. if (info.phone != null && info.phone.length() > 0)
    144. {
    145. String condition = String.format("phone='%s'", info.phone);
    146. tempList = query(condition);
    147. if (tempList.size() > 0)
    148. {
    149. update(info, condition);
    150. result = tempList.get(0).rowid;
    151. continue;
    152. }
    153. }
    154. // 不存在唯一性重复的记录,则插入新记录
    155. ContentValues cv = new ContentValues();
    156. cv.put("name", info.name);
    157. cv.put("age", info.age);
    158. cv.put("height", info.height);
    159. cv.put("weight", info.weight);
    160. cv.put("married", info.married);
    161. cv.put("update_time", info.update_time);
    162. cv.put("phone", info.phone);
    163. cv.put("password", info.password);
    164. // 执行插入记录动作,该语句返回插入记录的行号
    165. result = mDB.insert(TABLE_NAME, "", cv);
    166. if (result == -1) // 添加成功则返回行号,添加失败则返回-1
    167. {
    168. return result;
    169. }
    170. }
    171. return result;
    172. }
    173. // 根据条件更新指定的表记录
    174. public int update(UserInfo info, String condition)
    175. {
    176. ContentValues cv = new ContentValues();
    177. cv.put("name", info.name);
    178. cv.put("age", info.age);
    179. cv.put("height", info.height);
    180. cv.put("weight", info.weight);
    181. cv.put("married", info.married);
    182. cv.put("update_time", info.update_time);
    183. cv.put("phone", info.phone);
    184. cv.put("password", info.password);
    185. // 执行更新记录动作,该语句返回更新的记录数量
    186. return mDB.update(TABLE_NAME, cv, condition, null);
    187. }
    188. public int update(UserInfo info)
    189. {
    190. // 执行更新记录动作,该语句返回更新的记录数量
    191. return update(info, "rowid=" + info.rowid);
    192. }
    193. // 根据指定条件查询记录,并返回结果数据列表
    194. public List<UserInfo> query(String condition)
    195. {
    196. String sql = String.format("select rowid,_id,name,age,height,weight,married,update_time," +
    197. "phone,password from %s where %s;", TABLE_NAME, condition);
    198. Log.d(TAG, "query sql: " + sql);
    199. List<UserInfo> infoList = new ArrayList<UserInfo>();
    200. // 执行记录查询动作,该语句返回结果集的游标
    201. Cursor cursor = mDB.rawQuery(sql, null);
    202. // 循环取出游标指向的每条记录
    203. while (cursor.moveToNext())
    204. {
    205. UserInfo info = new UserInfo();
    206. info.rowid = cursor.getLong(0); // 取出长整型数
    207. info.xuhao = cursor.getInt(1); // 取出整型数
    208. info.name = cursor.getString(2); // 取出字符串
    209. info.age = cursor.getInt(3);
    210. info.height = cursor.getLong(4);
    211. info.weight = cursor.getFloat(5); // 取出浮点数
    212. //SQLite没有布尔型,用0表示false,用1表示true
    213. info.married = (cursor.getInt(6) == 0) ? false : true;
    214. info.update_time = cursor.getString(7);
    215. info.phone = cursor.getString(8);
    216. info.password = cursor.getString(9);
    217. infoList.add(info);
    218. }
    219. cursor.close(); // 查询完毕,关闭数据库游标
    220. return infoList;
    221. }
    222. // 根据手机号码查询指定记录
    223. public UserInfo queryByPhone(String phone)
    224. {
    225. UserInfo info = null;
    226. List<UserInfo> infoList = query(String.format("phone='%s'", phone));
    227. if (infoList.size() > 0)
    228. {
    229. info = infoList.get(0);
    230. }
    231. return info;
    232. }
    233. }

    2、编写内容提供器的基础字段类:UerInfoContent

    1. package com.example.myapplication.provider;
    2. import android.net.Uri;
    3. import android.provider.BaseColumns;
    4. import com.example.myapplication.database.UserDBHelper;
    5. public class UserInfoContent implements BaseColumns
    6. {
    7. // 这里的名称必须与AndroidManifest.xml里的android:authorities保持一致
    8. public static final String AUTHORITIES = "com.example.chapter07.provider.UserInfoProvider";
    9. // 内容提供器的外部表名
    10. public static final String TABLE_NAME = UserDBHelper.TABLE_NAME;
    11. // 访问内容提供器的URI
    12. public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITIES + "/user");
    13. // 下面是该表的各个字段名称
    14. public static final String USER_NAME = "name";
    15. public static final String USER_AGE = "age";
    16. public static final String USER_HEIGHT = "height";
    17. public static final String USER_WEIGHT = "weight";
    18. public static final String USER_MARRIED = "married";
    19. // 默认的排序方法
    20. public static final String DEFAULT_SORT_ORDER = "_id desc";
    21. }

    3、通过右键菜单,创建内容提供器:UserInfoProvider

    1. package com.example.myapplication.provider;
    2. import android.content.ContentProvider;
    3. import android.content.ContentUris;
    4. import android.content.ContentValues;
    5. import android.content.UriMatcher;
    6. import android.database.Cursor;
    7. import android.database.sqlite.SQLiteDatabase;
    8. import android.net.Uri;
    9. import com.example.myapplication.database.UserDBHelper;
    10. public class UserInfoProvider extends ContentProvider
    11. {
    12. private final static String TAG = "UserInfoProvider";
    13. private UserDBHelper userDB; // 声明一个用户数据库的帮助器对象
    14. public static final int USER_INFO = 1; // Uri匹配时的代号
    15. public static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    16. static { // 往Uri匹配器中添加指定的数据路径
    17. uriMatcher.addURI(UserInfoContent.AUTHORITIES, "/user", USER_INFO);
    18. }
    19. // 创建ContentProvider时调用,可在此获取具体的数据库帮助器实例
    20. @Override
    21. public boolean onCreate()
    22. {
    23. userDB = UserDBHelper.getInstance(getContext(), 1);
    24. return true;
    25. }
    26. // 插入数据
    27. @Override
    28. public Uri insert(Uri uri, ContentValues values)
    29. {
    30. if (uriMatcher.match(uri) == USER_INFO) // 匹配到了用户信息表
    31. {
    32. // 获取SQLite数据库的写连接
    33. SQLiteDatabase db = userDB.getWritableDatabase();
    34. // 向指定的表插入数据,返回记录的行号
    35. long rowId = db.insert(UserInfoContent.TABLE_NAME, null, values);
    36. if (rowId > 0) // 判断插入是否执行成功
    37. {
    38. // 如果添加成功,就利用新记录的行号生成新的地址
    39. Uri newUri = ContentUris.withAppendedId(UserInfoContent.CONTENT_URI, rowId);
    40. // 通知监听器,数据已经改变
    41. getContext().getContentResolver().notifyChange(newUri, null);
    42. }
    43. db.close(); // 关闭SQLite数据库连接
    44. }
    45. return uri;
    46. }
    47. // 根据指定条件删除数据
    48. @Override
    49. public int delete(Uri uri, String selection, String[] selectionArgs)
    50. {
    51. int count = 0;
    52. if (uriMatcher.match(uri) == USER_INFO) // 匹配到了用户信息表
    53. {
    54. // 获取SQLite数据库的写连接
    55. SQLiteDatabase db = userDB.getWritableDatabase();
    56. // 执行SQLite的删除操作,并返回删除记录的数目
    57. count = db.delete(UserInfoContent.TABLE_NAME, selection, selectionArgs);
    58. db.close(); // 关闭SQLite数据库连接
    59. }
    60. return count;
    61. }
    62. // 根据指定条件查询数据库
    63. @Override
    64. public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
    65. {
    66. Cursor cursor = null;
    67. if (uriMatcher.match(uri) == USER_INFO) // 匹配到了用户信息表
    68. {
    69. // 获取SQLite数据库的读连接
    70. SQLiteDatabase db = userDB.getReadableDatabase();
    71. // 执行SQLite的查询操作
    72. cursor = db.query(UserInfoContent.TABLE_NAME,
    73. projection, selection, selectionArgs, null, null, sortOrder);
    74. // 设置内容解析器的监听
    75. cursor.setNotificationUri(getContext().getContentResolver(), uri);
    76. }
    77. return cursor; // 返回查询结果集的游标
    78. }
    79. // 获取Uri支持的数据类型,暂未实现
    80. @Override
    81. public String getType(Uri uri)
    82. {
    83. throw new UnsupportedOperationException("Not yet implemented");
    84. }
    85. // 更新数据,暂未实现
    86. @Override
    87. public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
    88. {
    89. throw new UnsupportedOperationException("Not yet implemented");
    90. }
    91. }

     

    写数据:

    布局:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical"
    5. android:padding="5dp" >
    6. <RelativeLayout
    7. android:layout_width="match_parent"
    8. android:layout_height="40dp" >
    9. <TextView
    10. android:id="@+id/tv_name"
    11. android:layout_width="wrap_content"
    12. android:layout_height="match_parent"
    13. android:gravity="center"
    14. android:text="姓名:"
    15. android:textColor="@color/black"
    16. android:textSize="17sp" />
    17. <EditText
    18. android:id="@+id/et_name"
    19. android:layout_width="match_parent"
    20. android:layout_height="match_parent"
    21. android:layout_marginBottom="3dp"
    22. android:layout_marginTop="3dp"
    23. android:layout_toRightOf="@+id/tv_name"
    24. android:background="@drawable/editext_selector"
    25. android:gravity="left|center"
    26. android:hint="请输入姓名"
    27. android:inputType="text"
    28. android:maxLength="12"
    29. android:textColor="@color/black"
    30. android:textSize="17sp" />
    31. </RelativeLayout>
    32. <RelativeLayout
    33. android:layout_width="match_parent"
    34. android:layout_height="40dp" >
    35. <TextView
    36. android:id="@+id/tv_age"
    37. android:layout_width="wrap_content"
    38. android:layout_height="match_parent"
    39. android:gravity="center"
    40. android:text="年龄:"
    41. android:textColor="@color/black"
    42. android:textSize="17sp" />
    43. <EditText
    44. android:id="@+id/et_age"
    45. android:layout_width="match_parent"
    46. android:layout_height="match_parent"
    47. android:layout_marginBottom="3dp"
    48. android:layout_marginTop="3dp"
    49. android:layout_toRightOf="@+id/tv_age"
    50. android:background="@drawable/editext_selector"
    51. android:gravity="left|center"
    52. android:hint="请输入年龄"
    53. android:inputType="number"
    54. android:maxLength="2"
    55. android:textColor="@color/black"
    56. android:textSize="17sp" />
    57. </RelativeLayout>
    58. <RelativeLayout
    59. android:layout_width="match_parent"
    60. android:layout_height="40dp" >
    61. <TextView
    62. android:id="@+id/tv_height"
    63. android:layout_width="wrap_content"
    64. android:layout_height="match_parent"
    65. android:gravity="center"
    66. android:text="身高:"
    67. android:textColor="@color/black"
    68. android:textSize="17sp" />
    69. <EditText
    70. android:id="@+id/et_height"
    71. android:layout_width="match_parent"
    72. android:layout_height="match_parent"
    73. android:layout_marginBottom="3dp"
    74. android:layout_marginTop="3dp"
    75. android:layout_toRightOf="@+id/tv_height"
    76. android:background="@drawable/editext_selector"
    77. android:gravity="left|center"
    78. android:hint="请输入身高"
    79. android:inputType="number"
    80. android:maxLength="3"
    81. android:textColor="@color/black"
    82. android:textSize="17sp" />
    83. </RelativeLayout>
    84. <RelativeLayout
    85. android:layout_width="match_parent"
    86. android:layout_height="40dp" >
    87. <TextView
    88. android:id="@+id/tv_weight"
    89. android:layout_width="wrap_content"
    90. android:layout_height="match_parent"
    91. android:gravity="center"
    92. android:text="体重:"
    93. android:textColor="@color/black"
    94. android:textSize="17sp" />
    95. <EditText
    96. android:id="@+id/et_weight"
    97. android:layout_width="match_parent"
    98. android:layout_height="match_parent"
    99. android:layout_marginBottom="3dp"
    100. android:layout_marginTop="3dp"
    101. android:layout_toRightOf="@+id/tv_weight"
    102. android:background="@drawable/editext_selector"
    103. android:gravity="left|center"
    104. android:hint="请输入体重"
    105. android:inputType="numberDecimal"
    106. android:maxLength="5"
    107. android:textColor="@color/black"
    108. android:textSize="17sp" />
    109. </RelativeLayout>
    110. <Button
    111. android:id="@+id/btn_add_user"
    112. android:layout_width="match_parent"
    113. android:layout_height="wrap_content"
    114. android:gravity="center"
    115. android:text="添加用户信息"
    116. android:textColor="@color/black"
    117. android:textSize="17sp" />
    118. </LinearLayout>

    代码:

    1. package com.example.myapplication;
    2. import android.annotation.SuppressLint;
    3. import android.content.ContentValues;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.widget.EditText;
    7. import androidx.appcompat.app.AppCompatActivity;
    8. import com.example.myapplication.bean.UserInfo;
    9. import com.example.myapplication.provider.UserInfoContent;
    10. @SuppressLint("DefaultLocale")
    11. public class ContentWriteActivity extends AppCompatActivity implements View.OnClickListener
    12. {
    13. private static final String TAG = "ContentWriteActivity";
    14. private EditText et_name;
    15. private EditText et_age;
    16. private EditText et_height;
    17. private EditText et_weight;
    18. @Override
    19. protected void onCreate(Bundle savedInstanceState)
    20. {
    21. super.onCreate(savedInstanceState);
    22. setContentView(R.layout.activity_content_write);
    23. et_name = findViewById(R.id.et_name);
    24. et_age = findViewById(R.id.et_age);
    25. et_height = findViewById(R.id.et_height);
    26. et_weight = findViewById(R.id.et_weight);
    27. findViewById(R.id.btn_add_user).setOnClickListener(this);
    28. }
    29. @Override
    30. public void onClick(View v)
    31. {
    32. if (v.getId() == R.id.btn_add_user)
    33. {
    34. UserInfo user = new UserInfo();
    35. user.name = et_name.getText().toString();
    36. user.age = Integer.parseInt(et_age.getText().toString());
    37. user.height = Integer.parseInt(et_height.getText().toString());
    38. user.weight = Float.parseFloat(et_weight.getText().toString());
    39. addUser(user); // 添加一条用户记录
    40. }
    41. }
    42. // 添加一条用户记录
    43. private void addUser(UserInfo user)
    44. {
    45. ContentValues name = new ContentValues();
    46. name.put("name", user.name);
    47. name.put("age", user.age);
    48. name.put("height", user.height);
    49. name.put("weight", user.weight);
    50. name.put("married", 0);
    51. name.put("update_time", DateUtil.getNowDateTime(""));
    52. // 通过内容解析器往指定Uri添加用户信息
    53. getContentResolver().insert(UserInfoContent.CONTENT_URI, name);
    54. ToastUtil.show(this, "成功添加用户信息");
    55. }
    56. }

     

    读数据:

    布局:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical" >
    5. <Button
    6. android:id="@+id/btn_delete"
    7. android:layout_width="match_parent"
    8. android:layout_height="wrap_content"
    9. android:text="删除所有记录"
    10. android:textColor="@color/black"
    11. android:textSize="17sp" />
    12. <TextView
    13. android:id="@+id/tv_desc"
    14. android:layout_width="match_parent"
    15. android:layout_height="wrap_content"
    16. android:paddingLeft="5dp"
    17. android:textColor="@color/black"
    18. android:textSize="17sp" />
    19. <ScrollView
    20. android:layout_width="match_parent"
    21. android:layout_height="wrap_content">
    22. <LinearLayout
    23. android:id="@+id/ll_list"
    24. android:layout_width="match_parent"
    25. android:layout_height="wrap_content"
    26. android:orientation="vertical" />
    27. </ScrollView>
    28. </LinearLayout>

    代码:

    1. package com.example.myapplication;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.annotation.SuppressLint;
    4. import android.database.Cursor;
    5. import android.graphics.Color;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.widget.LinearLayout;
    9. import android.widget.TextView;
    10. import com.example.myapplication.bean.UserInfo;
    11. import com.example.myapplication.util.Utils;
    12. import com.example.myapplication.provider.UserInfoContent;
    13. import java.util.ArrayList;
    14. import java.util.List;
    15. @SuppressLint("DefaultLocale")
    16. public class ContentReadActivity extends AppCompatActivity implements View.OnClickListener
    17. {
    18. private static final String TAG = "ContentReadActivity";
    19. private TextView tv_desc;
    20. private LinearLayout ll_list; // 用户信息列表的线性布局
    21. @Override
    22. protected void onCreate(Bundle savedInstanceState)
    23. {
    24. super.onCreate(savedInstanceState);
    25. setContentView(R.layout.activity_content_read);
    26. findViewById(R.id.btn_delete).setOnClickListener(this);
    27. tv_desc = findViewById(R.id.tv_desc);
    28. ll_list = findViewById(R.id.ll_list);
    29. showAllUser(); // 显示所有的用户记录
    30. }
    31. // 显示所有的用户记录
    32. @SuppressLint("Range")
    33. private void showAllUser()
    34. {
    35. List<UserInfo> userList = new ArrayList<UserInfo>();
    36. // 通过内容解析器从指定Uri中获取用户记录的游标
    37. Cursor cursor = getContentResolver().query(UserInfoContent.CONTENT_URI, null, null, null, null);
    38. // 循环取出游标指向的每条用户记录
    39. while (cursor.moveToNext())
    40. {
    41. UserInfo user = new UserInfo();
    42. user.name = cursor.getString(cursor.getColumnIndex(UserInfoContent.USER_NAME));
    43. user.age = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_AGE));
    44. user.height = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_HEIGHT));
    45. user.weight = cursor.getFloat(cursor.getColumnIndex(UserInfoContent.USER_WEIGHT));
    46. userList.add(user); // 添加到用户信息列表
    47. }
    48. cursor.close(); // 关闭数据库游标
    49. String contactCount = String.format("当前共找到%d个用户", userList.size());
    50. tv_desc.setText(contactCount);
    51. ll_list.removeAllViews(); // 移除线性布局下面的所有下级视图
    52. for (UserInfo user : userList) // 遍历用户信息列表
    53. {
    54. String contactDesc = String.format("姓名为%s,年龄为%d,身高为%d,体重为%f\n", user.name, user.age, user.height, user.weight);
    55. TextView tv_contact = new TextView(this); // 创建一个文本视图
    56. tv_contact.setText(contactDesc);
    57. tv_contact.setTextColor(Color.BLACK);
    58. tv_contact.setTextSize(17);
    59. int pad = Utils.dip2px(this, 5);
    60. tv_contact.setPadding(pad, pad, pad, pad); // 设置文本视图的内部间距
    61. ll_list.addView(tv_contact); // 把文本视图添加至线性布局
    62. }
    63. }
    64. @Override
    65. public void onClick(View v)
    66. {
    67. if (v.getId() == R.id.btn_delete)
    68. {
    69. getContentResolver().delete(UserInfoContent.CONTENT_URI, "1=1", null);
    70. showAllUser();
    71. ToastUtil.show(this, "已删除所有记录");
    72. }
    73. }
    74. }

     

    Utils 
    
    1. package com.example.myapplication.util;
    2. import android.content.Context;
    3. public class Utils {
    4. // 根据手机的分辨率从 dp 的单位 转成为 px(像素)
    5. public static int dip2px(Context context, float dpValue) {
    6. // 获取当前手机的像素密度(1个dp对应几个px)
    7. float scale = context.getResources().getDisplayMetrics().density;
    8. return (int) (dpValue * scale + 0.5f); // 四舍五入取整
    9. }
    10. // 根据手机的分辨率从 px(像素) 的单位 转成为 dp
    11. public static int px2dip(Context context, float pxValue) {
    12. // 获取当前手机的像素密度(1个dp对应几个px)
    13. float scale = context.getResources().getDisplayMetrics().density;
    14. return (int) (pxValue / scale + 0.5f); // 四舍五入取整
    15. }
    16. }

    ToastUtil
    
    1. package com.example.myapplication.util;
    2. import android.content.Context;
    3. import android.widget.Toast;
    4. public class ToastUtil {
    5. public static void show(Context ctx, String desc) {
    6. Toast.makeText(ctx, desc, Toast.LENGTH_SHORT).show();
    7. }
    8. }

    DateUtil 
    
    1. package com.example.myapplication.util;
    2. import android.annotation.SuppressLint;
    3. import android.text.TextUtils;
    4. import java.text.SimpleDateFormat;
    5. import java.util.Calendar;
    6. import java.util.Date;
    7. @SuppressLint("SimpleDateFormat")
    8. public class DateUtil
    9. {
    10. // 获取当前的日期时间
    11. public static String getNowDateTime(String formatStr)
    12. {
    13. String format = formatStr;
    14. if (TextUtils.isEmpty(format))
    15. {
    16. format = "yyyyMMddHHmmss";
    17. }
    18. SimpleDateFormat sdf = new SimpleDateFormat(format);
    19. return sdf.format(new Date());
    20. }
    21. // 获取当前的时间
    22. public static String getNowTime()
    23. {
    24. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    25. return sdf.format(new Date());
    26. }
    27. // 获取当前的时间(精确到毫秒)
    28. public static String getNowTimeDetail()
    29. {
    30. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
    31. return sdf.format(new Date());
    32. }
    33. public static String formatDate(long time)
    34. {
    35. Date date = new Date(time);
    36. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    37. return sdf.format(date);
    38. }
    39. public static String getDate(Calendar calendar) {
    40. Date date = calendar.getTime();
    41. // 创建一个日期格式化的工具
    42. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    43. // 将当前日期时间按照指定格式输出格式化后的日期时间字符串
    44. return sdf.format(date);
    45. }
    46. public static String getMonth(Calendar calendar) {
    47. Date date = calendar.getTime();
    48. // 创建一个日期格式化的工具
    49. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    50. // 将当前日期时间按照指定格式输出格式化后的日期时间字符串
    51. return sdf.format(date);
    52. }
    53. public static Date formatString(String strTime) {
    54. Date date = new Date();
    55. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    56. try {
    57. date = sdf.parse(strTime);
    58. } catch (Exception e) {
    59. e.printStackTrace();
    60. }
    61. return date;
    62. }
    63. }

     

     

    ========================================================================================================

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    算法-26. 删除有序数组中的重复项-⭐
    什么是闭包
    gradle编译spring源码过程问题整理
    干货 | 查资料利器:线上图书馆
    XSS漏洞介绍
    第十三届蓝桥杯省赛 C++ C 组 E 题、Python B组 D题、PythonC组 D 题—— 数位排序(AC)
    linux安装mysql
    探索Headless组件与Tailwind CSS的魔力——前端开发的新选择
    Flutter学习4-基本UI组件
    港联证券:资金融通构成强支撑 “一带一路”金融合作开新局
  • 原文地址:https://blog.csdn.net/m0_61442607/article/details/126453568