题目:
1.创建工程(10分)
2.实现登录页面(10分)
3.实现数据库储存(10分)
4.建立一个表(10分)
5.实现增删改(10分)
6.根据各种条件查询(10分)
7.把查询的数据展示到listview上(10分)
8.创建Notitfcation(10分)
9.增删改成功后发送Notitfcation通知(10分)
10.注释(10分)
代码:
布局代码:
activity_main.xml文件:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:orientation="horizontal"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:textSize="20sp" /> android:id="@+id/userName_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" /> android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:orientation="horizontal"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:" android:textSize="20sp" /> android:id="@+id/userPassword_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" />
activity_home.xml文件:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" >
item_layout.xml文件:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> android:id="@+id/name_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:textColor="@android:color/black" android:textSize="20sp" /> android:id="@+id/password_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginBottom="10dp" android:textColor="@android:color/black" android:textSize="18sp" />
dialog_layout.xml文件:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> android:id="@+id/dialog_name_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:hint="请输入用户名" /> android:id="@+id/dialog_password_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:hint="请输入密码" /> android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp">
Java代码:
Config:
public class Config { //数据库的名字 public static final String DB_NAME = "user.db"; //数据库中表的名字 public static final String TABLE_NAME = "user"; //版本号 public static final int DB_VERSION = 1; }
SQLiteHelper:
public class SQLiteHelper extends SQLiteOpenHelper { public SQLiteHelper(Context context ) { super(context, Config.DB_NAME, null, Config.DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table if not exists user(_id integer primary key autoincrement,name varchar(20),password varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
DBUtils:
public class DBUtils { private SQLiteHelper helper; private static DBUtils dbUtils; private DBUtils(Context context) { helper = new SQLiteHelper(context); } /** * 单例 * * @param context * @return */ public static synchronized DBUtils getDButils(Context context) { if (dbUtils == null) { dbUtils = new DBUtils(context); } return dbUtils; } /** * 向数据库中插入数据 * * @param name * @param password * @return */ public boolean insertUser(String name, String password) { SQLiteDatabase db = helper.getReadableDatabase(); ContentValues values = new ContentValues(); values.put("name", name); values.put("password", password); return db.insert(Config.TABLE_NAME, null, values) > 0; } /** * 查询全部的用户信息 * * @return 游标 */ public Cursor getAllUser() { SQLiteDatabase db = helper.getReadableDatabase(); return db.rawQuery("select * from user", null); } /** * 删除用户 * * @param name * @return */ public boolean deleteUser(String name) { SQLiteDatabase db = helper.getReadableDatabase(); return db.delete(Config.TABLE_NAME, "name = ?", new String[]{name}) > 0; } /** * 修改数据 * * @param name * @param password * @return */ public boolean upDataUser(String name, String password) { SQLiteDatabase db = helper.getReadableDatabase(); ContentValues values = new ContentValues(); values.put("password", password); return db.update(Config.TABLE_NAME, values, "name = ?", new String[]{name}) > 0; } }
NotificationManager:
public class NotificationManager { //发送一个通知的工具类方法 public static void sendNotification(Context context,String info){ android.app.NotificationManager manager = (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(context); builder.setContentTitle("提示"); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentText(info); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { manager.notify(10,builder.build()); } } }
MainActivity:
public class MainActivity extends AppCompatActivity { private EditText userNameEt; private EditText userPasswordEt; private DBUtils dbUtils; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbUtils = DBUtils.getDButils(this); userNameEt = (EditText) findViewById(R.id.userName_et); userPasswordEt = (EditText) findViewById(R.id.userPassword_et); //如果曾经登录过则直接跳转到下一个页面 Cursor allUser = dbUtils.getAllUser(); if (allUser!=null&&allUser.getCount()>0){ Intent intent = new Intent(this, HomeActivity.class); startActivity(intent); } } public void onClick(View view) { //点击后 将数据插入到数据库中 并且跳转到主页面 String userName = userNameEt.getText().toString(); String userPassword = userPasswordEt.getText().toString(); if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(userPassword)) { Toast.makeText(this, "请输入用户名或密码", Toast.LENGTH_SHORT).show(); return; } //向数据库中插入一条数据 boolean isSuccess = dbUtils.insertUser(userName, userPassword); if (isSuccess) { //发送一个通知 NotificationManager.sendNotification(this, "插入用户信息到数据库成功!"); Toast.makeText(this, "数据添加成功", Toast.LENGTH_SHORT).show(); } //跳转到主页面 Intent intent = new Intent(this, HomeActivity.class); startActivity(intent); } }
HomeActivity:
public class HomeActivity extends AppCompatActivity { private ListView listView; private DBUtils dButils; private SimpleCursorAdapter adapter; private AlertDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_empty); listView = (ListView) findViewById(R.id.listView); dButils = DBUtils.getDButils(this); Cursor allUser = dButils.getAllUser(); adapter = new SimpleCursorAdapter(this, R.layout.item_layout, allUser, new String[]{"name", "password"}, new int[]{R.id.name_tv, R.id.password_tv}, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listView.setAdapter(adapter); //给listView 注册上下文菜单 registerForContextMenu(listView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(1, 1, 0, "添加数据"); menu.add(1, 2, 0, "删除数据"); menu.add(1, 3, 0, "修改数据"); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); int position = menuInfo.position; switch (item.getItemId()) { case 1: //添加数据 showDialog(true); break; case 2: //删除数据 Cursor cursor = (Cursor) adapter.getItem(position); String name = cursor.getString(cursor.getColumnIndex("name")); boolean isSuccess = dButils.deleteUser(name); if (isSuccess) { Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show(); NotificationManager.sendNotification(this, "删除条目成功"); } //刷新视图 adapter.changeCursor(dButils.getAllUser()); break; case 3: //修改数据 showDialog(false); break; } return super.onContextItemSelected(item); } public void showDialog(final boolean isInsert) { View view = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null); final EditText nameEt = (EditText) view.findViewById(R.id.dialog_name_et); final EditText passwordEt = (EditText) view.findViewById(R.id.dialog_password_et); AlertDialog.Builder builder = new AlertDialog.Builder(this); if (isInsert) { builder.setTitle("插入用户"); } else { builder.setTitle("修改密码"); } builder.setIcon(R.mipmap.ic_launcher); //取消按钮 view.findViewById(R.id.bt_cancel).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); //确认按钮 view.findViewById(R.id.bt_ok).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = nameEt.getText().toString(); String password = passwordEt.getText().toString(); boolean isSuccess; if (isInsert) { isSuccess = dButils.insertUser(name, password); } else { isSuccess = dButils.upDataUser(name, password); } if (isInsert) { Toast.makeText(HomeActivity.this, "添加成功", Toast.LENGTH_SHORT).show(); NotificationManager.sendNotification(HomeActivity.this, "添加用户信息成功"); } else { Toast.makeText(HomeActivity.this, "修改成功", Toast.LENGTH_SHORT).show(); NotificationManager.sendNotification(HomeActivity.this, "修改用户信息成功"); } //刷新视图 adapter.changeCursor(dButils.getAllUser()); dialog.dismiss(); } }); builder.setView(view); dialog = builder.show(); } }