题干:
1. 写一个广播,在一个界面点击按钮发送广播
2. 在另一个界面接收广播,并弹出一个吐司
3. 在弹出吐司的同时,创建一个数据库
4. 数据库中有一个表,表里有几个字段,必有的字段分别是【我是谁】【我在哪】【我从哪里来】
5. 添加两个学生,并给三个必有的字段赋值
6. 删除一名学生
7. 查询剩余学生的的【我是谁】字段
8. 改变剩余学生的【我在哪】字段值为【八维游戏】
布局代码:
layout_main.xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" tools:context="com.mrzhao.zhoukaodemo.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="点击发送广播" /> </LinearLayout>
layout_second.xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" tools:context="com.mrzhao.zhoukaodemo.SecondActivity"> <Button android:id="@+id/delete_bt" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="删除一名学生" /> <Button android:id="@+id/query_bt" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="查询剩余学生的我是谁" /> <Button android:id="@+id/upData_bt" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="改变剩余学生的我在那" /> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </LinearLayout>
item_layout.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"> <TextView android:id="@+id/name_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:textSize="20sp" /> <TextView android:id="@+id/who_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:textSize="18sp" /> <TextView android:id="@+id/where_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:textSize="18sp" /> <TextView android:id="@+id/from_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:textSize="18sp" /> </LinearLayout>
Config:
public class Config { public static final String DB_NAME = "student.db"; public static final String TABLE_NAME = "student"; public static final int DB_VERSION = 1; }
SQLiteHelper
public class SQLiteHelp extends SQLiteOpenHelper { public SQLiteHelp(Context context) { super(context, Config.DB_NAME, null, Config.DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table if not exists student(_id integer primary key autoincrement,name varchar(10),who varchar(10),stuwhere varchar(20),stufrom varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
DBUtils文件:
public class DBUtils { private static DBUtils dbUtils; private SQLiteHelp helper; private DBUtils(Context context) { helper = new SQLiteHelp(context); } public static DBUtils getInstance(Context context) { if (dbUtils == null) { dbUtils = new DBUtils(context); } return dbUtils; } /** * 查询全部剩余学生 * * @return */ public Cursor getAllStudent() { SQLiteDatabase db = helper.getReadableDatabase(); return db.query(Config.TABLE_NAME, null, null, null, null, null, null, null); } /** * 根据名字删除学生 */ public boolean deleteStudent(String name) { SQLiteDatabase db = helper.getReadableDatabase(); return db.delete(Config.TABLE_NAME, "name = ?", new String[]{name}) > 0; } /** * 插入学生数据 * * @param name * @param who * @param where * @param from * @return */ public boolean insertStudent(String name, String who, String where, String from) { SQLiteDatabase db = helper.getReadableDatabase(); ContentValues values = new ContentValues(); values.put("name", name); values.put("who", who); values.put("stuwhere", where); values.put("stufrom", from); return db.insert(Config.TABLE_NAME, null, values) > 0; } /** * 查询剩余学生的我是谁字段 * * @return */ public List<String> getWho() { List<String> list = new ArrayList<>(); SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from " + Config.TABLE_NAME, null); if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { String who = cursor.getString(cursor.getColumnIndex("who")); list.add(who); } } return list; } /** *更新所有数据我在那的数据为 八维游戏 */ public void upDataWhere() { SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from " + Config.TABLE_NAME, null); if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("name")); ContentValues values = new ContentValues(); values.put("stuwhere", "八维游戏"); db.update(Config.TABLE_NAME, values, "name = ?", new String[]{name}); } } } }
MyReceiver文件:
public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //收到发送过来的广播 String info = intent.getStringExtra("info"); //弹出一个土司 Toast.makeText(context, info, Toast.LENGTH_SHORT).show(); //接到广播后 打开第二个页面 Intent intent1 = new Intent(context, SecondActivity.class); context.startActivity(intent1); } }
MainActivity:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View view) { //点击按钮发送一个广播 Intent intent = new Intent(); intent.setAction("com.receiver.zhoukao"); intent.putExtra("info","广播广播,北京下雪了!"); sendBroadcast(intent); } }
SecondActivity文件:
public class SecondActivity extends AppCompatActivity { private ListView listView; private DBUtils dbUtils; private SimpleCursorAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); listView = (ListView) findViewById(R.id.listView); //创建一个数据库 dbUtils = DBUtils.getInstance(SecondActivity.this); //插入三条数据 dbUtils.insertStudent("王智勇", "大宝贝", "唐山", "漠河"); dbUtils.insertStudent("张磊", "磊磊", "北京", "内蒙古"); dbUtils.insertStudent("崔瑞骏", "小崔", "北京", "运城"); //插入数据后查询一下 Cursor allStudent = dbUtils.getAllStudent(); //获取数据库中的数据展示出来 adapter = new SimpleCursorAdapter(this, R.layout.item_layout, allStudent, new String[]{"name", "who", "stuwhere", "stufrom"}, new int[]{R.id.name_tv, R.id.who_tv, R.id.where_tv, R.id.from_tv}, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listView.setAdapter(adapter); } public void onClick(View view) { switch (view.getId()) { case R.id.query_bt: //查询所有学生的 我是谁字段 List<String> who = dbUtils.getWho(); Toast.makeText(this, who.toString(), Toast.LENGTH_SHORT).show(); break; case R.id.delete_bt: //删除一个学生 boolean isSuccess = dbUtils.deleteStudent("王智勇"); if (isSuccess) { Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show(); Cursor allStudent = dbUtils.getAllStudent(); adapter.changeCursor(allStudent); } else { Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show(); } break; case R.id.upData_bt: //更新所有学生的 我在哪 为 八维游戏 dbUtils.upDataWhere(); Cursor allStudent = dbUtils.getAllStudent(); adapter.changeCursor(allStudent); break; } } }