个人冲刺(四)——体温上报app(二阶段)
冲刺任务:完成用户注册功能和数据库类
RegisterActivity.java
package com.example.helloworld; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; /** * Created by littlecurl 2018/6/24 */ /** * 此类 implements View.OnClickListener 之后, * 就可以把onClick事件写到onCreate()方法之外 * 这样,onCreate()方法中的代码就不会显得很冗余 */ public class RegisterActivity extends AppCompatActivity implements View.OnClickListener { private String realCode; private DBOpenHelper mDBOpenHelper; private Button mBtRegisteractivityRegister; private RelativeLayout mRlRegisteractivityTop; private ImageView mIvRegisteractivityBack; private LinearLayout mLlRegisteractivityBody; private EditText mEtRegisteractivityUsername; private EditText mEtRegisteractivityUserid; private EditText mEtRegisteractivityUserphone; private EditText mEtRegisteractivityUseraddress; private EditText mEtRegisteractivityPhonecodes; private ImageView mIvRegisteractivityShowcode; private RelativeLayout mRlRegisteractivityBottom; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); initView(); mDBOpenHelper = new DBOpenHelper(this); //将验证码用图片的形式显示出来 mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap()); realCode = Code.getInstance().getCode().toLowerCase(); } private void initView(){ mBtRegisteractivityRegister = findViewById(R.id.bt_registeractivity_register); mRlRegisteractivityTop = findViewById(R.id.rl_registeractivity_top); mIvRegisteractivityBack = findViewById(R.id.iv_registeractivity_back); mLlRegisteractivityBody = findViewById(R.id.ll_registeractivity_body); mEtRegisteractivityUsername = findViewById(R.id.et_registeractivity_name); mEtRegisteractivityUserid = findViewById(R.id.et_registeractivity_id); mEtRegisteractivityUserphone = findViewById(R.id.et_registeractivity_phone); mEtRegisteractivityUseraddress = findViewById(R.id.et_registeractivity_address); mEtRegisteractivityPhonecodes = findViewById(R.id.et_registeractivity_phoneCodes); mIvRegisteractivityShowcode = findViewById(R.id.iv_registeractivity_showCode); mRlRegisteractivityBottom = findViewById(R.id.rl_registeractivity_bottom); /** * 注册页面能点击的就三个地方 * top处返回箭头、刷新验证码图片、注册按钮 */ mIvRegisteractivityBack.setOnClickListener(this); mIvRegisteractivityShowcode.setOnClickListener(this); mBtRegisteractivityRegister.setOnClickListener(this); } public void onClick(View view) { switch (view.getId()) { case R.id.iv_registeractivity_back: //返回登录页面 Intent intent1 = new Intent(this, loginActivity.class); startActivity(intent1); finish(); break; case R.id.iv_registeractivity_showCode: //改变随机验证码的生成 mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap()); realCode = Code.getInstance().getCode().toLowerCase(); break; case R.id.bt_registeractivity_register: //注册按钮 //获取用户输入的用户名、密码、验证码 String username = mEtRegisteractivityUsername.getText().toString().trim(); String userid = mEtRegisteractivityUserid.getText().toString().trim(); String userphone = mEtRegisteractivityUserphone.getText().toString().trim(); String useraddress = mEtRegisteractivityUseraddress.getText().toString().trim(); String phoneCode = mEtRegisteractivityPhonecodes.getText().toString().toLowerCase(); //注册验证 if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(userid) && !TextUtils.isEmpty(phoneCode) && !TextUtils.isEmpty(userphone) && !TextUtils.isEmpty(useraddress) ) { if (phoneCode.equals(realCode)) { //将用户名和密码加入到数据库中 mDBOpenHelper.add(username, userid,userphone,useraddress); Intent intent2 = new Intent(this, MainActivity.class); startActivity(intent2); finish(); Toast.makeText(this, "验证通过,注册成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "验证码错误,注册失败", Toast.LENGTH_SHORT).show(); } }else { Toast.makeText(this, "未完善信息,注册失败", Toast.LENGTH_SHORT).show(); } break; } } }
MyDBHelper.java
package com.example.helloworld; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.widget.Toast; import androidx.annotation.Nullable; public class MyDBHelper extends SQLiteOpenHelper { private Context context; public MyDBHelper(Context context,String name,int version) { super(context, name, null, version); this.context=context; } @Override public void onCreate(SQLiteDatabase db) { String sql="create table IF NOT EXISTS personwendu(id integer primary key autoincrement, address varchar, name varchar, wendu varchar, dateandtime varchar,more varchar)"; db.execSQL(sql); Toast.makeText(context,"数据加入成功",Toast.LENGTH_LONG).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
DBOpenHelper.java
package com.example.helloworld; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import java.util.ArrayList; public class DBOpenHelper extends SQLiteOpenHelper { /** * 声明一个AndroidSDK自带的数据库变量db */ private SQLiteDatabase db; /** * 写一个这个类的构造函数,参数为上下文context,所谓上下文就是这个类所在包的路径 * 指明上下文,数据库名,工厂默认空值,版本号默认从1开始 * super(context,"db_test",null,1); * 把数据库设置成可写入状态,除非内存已满,那时候会自动设置为只读模式 * 不过,以现如今的内存容量,估计一辈子也见不到几次内存占满的状态 * db = getReadableDatabase(); */ public DBOpenHelper(Context context){ super(context,"db_test",null,1); db = getReadableDatabase(); } /** * 重写两个必须要重写的方法,因为class DBOpenHelper extends SQLiteOpenHelper * 而这两个方法是 abstract 类 SQLiteOpenHelper 中声明的 abstract 方法 * 所以必须在子类 DBOpenHelper 中重写 abstract 方法 * 想想也是,为啥规定这么死必须重写? * 因为,一个数据库表,首先是要被创建的,然后免不了是要进行增删改操作的 * 所以就有onCreate()、onUpgrade()两个方法 * @param db */ @Override public void onCreate(SQLiteDatabase db){ db.execSQL("CREATE TABLE IF NOT EXISTS user(" + "_id INTEGER PRIMARY KEY AUTOINCREMENT," + "username TEXT," + "userid TEXT,"+ "userphone TEXT,"+ "useraddress TEXT)" ); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ db.execSQL("DROP TABLE IF EXISTS user"); onCreate(db); } /** * 接下来写自定义的增删改查方法 * 这些方法,写在这里归写在这里,以后不一定都用 * add() * delete() * update() * getAllData() */ public void add(String username,String userid,String userphone,String useraddress){ db.execSQL("INSERT INTO user (username,userid,userphone,useraddress) VALUES(?,?,?,?)", new Object[]{username,userid,userphone,useraddress}); } public void delete(String userphone){ db.execSQL("DELETE FROM user WHERE userphone = "+userphone); } public void updata(String userphone){ db.execSQL("UPDATE user SET userphone = ?",new Object[]{userphone}); } /** * 前三个没啥说的,都是一套的看懂一个其他的都能懂了 * 下面重点说一下查询表user全部内容的方法 * 我们查询出来的内容,需要有个容器存放,以供使用, * 所以定义了一个ArrayList类的list * 有了容器,接下来就该从表中查询数据了, * 这里使用游标Cursor,这就是数据库的功底了, * 在Android中我就不细说了,因为我数据库功底也不是很厚, * 但我知道,如果需要用Cursor的话,第一个参数:"表名",中间5个:null, * 最后是查询出来内容的排序方式:"name DESC" * 游标定义好了,接下来写一个while循环,让游标从表头游到表尾 * 在游的过程中把游出来的数据存放到list容器中 * @return */ public ArrayList<User> getAllData(){ ArrayList<User> list = new ArrayList<User>(); Cursor cursor = db.query("user",null,null,null,null,null,"username DESC"); cursor.moveToFirst(); while(cursor.moveToNext()){ String username = cursor.getString(cursor.getColumnIndex("username")); String userid = cursor.getString(cursor.getColumnIndex("userid")); String userphone = cursor.getString(cursor.getColumnIndex("userphone")); String useraddress = cursor.getString(cursor.getColumnIndex("useraddress")); list.add(new User(username,userid,userphone,useraddress)); } Log.v("输出数据库查询结果:",list.toString()); return list; } }