昨天:完善注册界面
今天:完成注册用户名是否重复
package com.example.lenovo.share;import android.content.ContentValues;import android.content.Intent;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class RegisterActivity extends AppCompatActivity { //返回按钮 ,,,,等等写 private TextView back; //注册按钮 private Button register; //控件:用户名,密码,再次输入密码 private EditText et_username,et_psssword,et_repassword; //用户名,密码,再次输入密码的获取值 private String username,password,repassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); //调用init方法 init(); } private void init() { //从activity_regisiter.xml页面获取对应的控件 et_username=findViewById(R.id.regisiter_username); et_psssword=findViewById(R.id.regisiter_password); et_repassword=findViewById(R.id.regisiter_repassword); register=findViewById(R.id.regisiter_zhuce); //注册按钮 register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取输入在相应控件的字符串 getEditToString(); //判断输入内容 if(TextUtils.isEmpty(username)){ Toast.makeText(RegisterActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show(); return; }else if(TextUtils.isEmpty(password)){ Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show(); return; }else if(TextUtils.isEmpty(repassword)){ Toast.makeText(RegisterActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show(); return; } else if(isExistUserName(username)){ Toast.makeText(RegisterActivity.this, "此账户名已经存在", Toast.LENGTH_SHORT).show(); return; }else{ Toast.makeText(RegisterActivity.this, username, Toast.LENGTH_SHORT).show(); Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); add(); Intent data = new Intent(); data.putExtra("username", username); setResult(RESULT_OK, data); //RESULT_OK为Activity系统常量,状态码为-1, //表示此页面下的内容操作成功将data返回到上一页面,如果是用back返回过去的则不存在用setResult传递data值 RegisterActivity.this.finish(); } } }); } private void getEditToString() { username=et_username.getText().toString().trim(); password=et_psssword.getText().toString().trim(); repassword=et_repassword.getText().toString().trim(); } //添加数据到数据库 public void add(){ //把账号、密码保存到数据库里面 ContentValues values= new ContentValues(); values.put("username",username); values.put("password",password); //调用DBOpenHelper (share.db是创建的数据库的名称) DbHelper helper = new DbHelper(this,"share.db",null,1); SQLiteDatabase db = helper.getWritableDatabase(); long rowid = db.insert("user",null,values); } //查询数据库中是否有当前用户名 private boolean isExistUserName(String username){ boolean has_userName=false; //调用DBOpenHelper (share.db是创建的数据库的名称) DbHelper helper = new DbHelper(this,"share.db",null,1); SQLiteDatabase db = helper.getWritableDatabase(); //根据输入的账号去数据库中进行查询(user是表名) Cursor c = db.query("user",null,"username=?",new String[]{username},null,null,null); //如果有查询到数据 if(c!=null && c.getCount() >= 1){ Toast.makeText(getApplicationContext(),"姓名"+c.getString(c.getColumnIndex("username")),Toast.LENGTH_SHORT).show(); //可以把查询出来的值打印出来在后台显示/查看 String[] cols = c.getColumnNames(); while(c.moveToNext()){ for(String ColumnName:cols){ Log.i("info",ColumnName+":"+c.getString(c.getColumnIndex(ColumnName))); } } has_userName=true; c.close(); db.close(); this.finish(); } return has_userName; }}
问题:数据库信息循环遍历输出没有解决