基于SQLite的安卓平台用户信息管理系统编程实例,开源一下
package com.example.administrator.sqlite;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn = null;
Button btn_yinshi = null;
private static final String DB_NAME = "dbUser.db";//定义DB_NAME
private SQLiteDatabase db;//SQLiteDatabase对象db
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册
btn = ((Button) findViewById(R.id.btn));
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),UserRegs.class);
startActivity(intent);
}
});
//登入
btn_yinshi = ((Button) findViewById(R.id.btn_yinshi));
btn_yinshi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),UserLogin.class);
startActivity(intent);
}
});
}
}
package com.example.administrator.sqlite;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn = null;
Button btn_yinshi = null;
private static final String DB_NAME = "dbUser.db";//定义DB_NAME
private SQLiteDatabase db;//SQLiteDatabase对象db
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册
btn = ((Button) findViewById(R.id.btn));
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),UserRegs.class);
startActivity(intent);
}
});
//登入
btn_yinshi = ((Button) findViewById(R.id.btn_yinshi));
btn_yinshi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),UserLogin.class);
startActivity(intent);
}
});
}
}
package com.example.administrator.sqlite;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 UserLogin extends AppCompatActivity {
private static final String DB_NAME = "dbUser.db";
private SQLiteDatabase db;
private EditText etUserName=null;
private EditText etPwd=null;
private Button btnLogin=null;
private Button btnReg=null;
private TextView tvShowInfo=null;
private String strUserName="";
private String strPwd="";
private int iUT=-1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
etUserName=(EditText)findViewById(R.id.etULUserName);
etPwd=(EditText)findViewById(R.id.etULPwd);
btnLogin=(Button)findViewById(R.id.btnULLogin);
btnReg=(Button)findViewById(R.id.btnULReg);
tvShowInfo=(TextView)findViewById(R.id.tvULShowInfo);
OpenCreateDB();
Button.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.getId() == R.id.btnULLogin)
{
strUserName=etUserName.getText().toString();
strPwd=etPwd.getText().toString();
if(isStrEmpty(strUserName) == false)
{
if(isStrEmpty(strPwd) == false)
{
if(isValidUser(strUserName,strPwd) == true)
{
//Toast.makeText(UserLogin.this, "用户登录成功!", Toast.LENGTH_SHORT).show();
//Toast.makeText(UserLogin.this, "用户登录失败!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),Welcome.class);
intent.putExtra("name",strUserName);
startActivity(intent);
//showUserType(strUserName,strPwd);
}
else
{
Toast.makeText(UserLogin.this, "用户登录失败!", Toast.LENGTH_SHORT).show();
tvShowInfo.setText("用户登录失败");
}
}
else
{
Toast.makeText(UserLogin.this, "密码不可为空!", Toast.LENGTH_SHORT).show();
tvShowInfo.setText("密码不可为空");
etPwd.setFocusable(true);
}
}
else
{
Toast.makeText(UserLogin.this, "用户名不可为空!", Toast.LENGTH_SHORT).show();
tvShowInfo.setText("用户名不可为空");
etUserName.setFocusable(true);
}
}
else if(v.getId() == R.id.btnULReg)
{
Intent intent = new Intent();
intent.setClass(UserLogin.this,UserRegs.class);
startActivity(intent);
}
}
};
btnLogin.setOnClickListener(listener);
btnReg.setOnClickListener(listener);
}
private void showUserType(String strUserName,String strUserPwd) {
iUT = getUserType(strUserName,strUserPwd);
if(iUT == 1)
{
Toast.makeText(UserLogin.this, "您是系统管理员!", Toast.LENGTH_SHORT).show();
}
else if(iUT == 0)
{
Toast.makeText(UserLogin.this, "您是普通用户!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(UserLogin.this, "出错啦!", Toast.LENGTH_SHORT).show();
}
}
private int getUserType(String strUserName, String strUserPwd) {
int igut = -1;
Cursor cursor = db.rawQuery("select * from tuserinfo where username='"+strUserName+"' and userpwd='"+strUserPwd+"'",null);
if(cursor.getCount() == 1)
{
igut = cursor.getInt(cursor.getColumnIndex("usertype"));
cursor.close();
}
else
{
cursor.close();
igut = 99;
}
return igut;
}
private void OpenCreateDB() {
try
{
db = openOrCreateDatabase(DB_NAME, this.MODE_PRIVATE, null);
}
catch (Throwable e)
{
Log.e("tag","openDatabase error:" + e.getMessage());
db=null;
}
try
{
db.execSQL("CREATE TABLE IF NOT EXISTS tuserinfo (_id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR, userpwd VARCHAR, usertype INTEGER)");
}
catch (SQLException se)
{
String msg = "doInstall.error:[%s].%s";
Log.d("tag",String.format(msg, se.getClass(),se.getMessage()));
}
}
private boolean isStrEmpty(String strInput)
{
if(strInput.equals(""))
{
return true;
}
else
{
return false;
}
}
private boolean isValidUser(String strUserName,String strUserPwd)
{
Cursor cursor=db.rawQuery("select * from tuserinfo where username='"+strUserName+"' and userpwd='"+strUserPwd+"'",null);
if(cursor.getCount()== 1)
{
cursor.close();
return true;
}
else
{
cursor.close();
return false;
}
}
protected void onDestroy()
{
super.onDestroy();
if(db!=null)
{
db.close();
}
}
}
<FrameLayout 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.example.administrator.sqlite.UserLogin">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<TextView
android:id="@+id/tvULTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="欢迎登录"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tvULUserName"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="用户名:" />
<EditText
android:id="@+id/etULUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入用户名"
android:inputType="textPersonName" >
<requestFocus />
EditText>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tvULPwd"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="密码:" />
<EditText
android:id="@+id/etULPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入密码"
android:inputType="textPassword" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnULLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登录" />
<Button
android:id="@+id/btnULReg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="新用户" />
LinearLayout>
<TextView
android:id="@+id/tvULShowInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="信息提示区" />
LinearLayout>
FrameLayout>
package com.example.administrator.sqlite;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 UserRegs extends AppCompatActivity {
private static final String DB_NAME="dbUser.db";
private SQLiteDatabase db;
private Button btnOk=null;
private Button btnCancel=null;
private EditText etUserName=null;
private EditText etPwd=null;
private EditText etRePwd=null;
private TextView tvShowInfo=null;
private String strUserName="";
private String strPwd="";
private String strRePwd="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_regs2);
etUserName=(EditText)findViewById(R.id.etURUserName);
etPwd=(EditText)findViewById(R.id.etURPwd);
etRePwd=(EditText)findViewById(R.id.etURRePwd);
btnOk=(Button)findViewById(R.id.btnOk);
btnCancel=(Button)findViewById(R.id.btnCancel);
OpenCreateDB();
Button.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.getId() == R.id.btnOk)
{
//获取编辑框的数据
strUserName=etUserName.getText().toString();
strPwd=etPwd.getText().toString();
strRePwd=etRePwd.getText().toString();
if(isStrEmpty(strUserName) == false)
{
if(isStrEmpty(strPwd) == false)
{
if(isStrEmpty(strRePwd) == false)
{
if(isPwdSame(strPwd,strRePwd) == true)
{
insertUserInfo(strUserName,strPwd);
}
else
{
tvShowInfo.setText("密码和确认密码不一致");
}
}
else
{
tvShowInfo.setText("确认密码不可为空");
etRePwd.setFocusable(true);
}
}
else
{
tvShowInfo.setText("密码不可为空");
etPwd.setFocusable(true);
}
}
else
{
tvShowInfo.setText("用户名不可为空");
etUserName.setFocusable(true);
}
}
else if(v.getId() == R.id.btnCancel)
{
etUserName.setText("");
etPwd.setText("");
etRePwd.setText("");
etUserName.setFocusable(true);
tvShowInfo.setText("");
}
}
};
btnOk.setOnClickListener(listener);
btnCancel.setOnClickListener(listener);
}
private boolean isExistAdmin()
{
//将usertype=1的管理员用户查找出来
Cursor cursor=db.rawQuery("select * from tuserinfo where usertype=1",null);
if(cursor.getCount()> 0)//存在管理员用户
{
cursor.close();
return true;
}
else//不存在管理员用户
{
cursor.close();
return false;
}
}
private void insertAdminInfo()
{
String strUserName= "[email protected]";
String strUserPwd="1";
int iUserType=1;
if(isExistAdmin()==false)//若表中目前还没有管理员用户
{
ContentValues cvUserInfo = new ContentValues();
cvUserInfo.put("username", strUserName);
cvUserInfo.put("userpwd", strUserPwd);
cvUserInfo.put("usertype", iUserType);
if(db!=null)
{
db.insert("tuserinfo", null, cvUserInfo);
Log.d("msg","插入结束");
Toast.makeText(UserRegs.this, "注册成功!", Toast.LENGTH_SHORT).show();
tvShowInfo.setText("用户名:"+strUserName+"\n"+"密码:"+strUserPwd+"\n"+"类别:"+iUserType);
}
}
else
{
tvShowInfo.setText("已存在系统管理员\n用户名:"+strUserName+"\n"+"密码:"+strUserPwd+"\n");
}
}
private void insertUserInfo(String strUserName,String strUserPwd) {
int iUserType=0;
if(isExistUserName(strUserName) == false)
{
ContentValues contentvalues = new ContentValues();
contentvalues.put("username", strUserName);
contentvalues.put("userpwd", strUserPwd);
contentvalues.put("usertype", iUserType);
if(db != null)
{
db.insert("tuserinfo",null,contentvalues);
Toast.makeText(UserRegs.this, "注册成功!", Toast.LENGTH_SHORT).show();
tvShowInfo.setText("已经成功注册普通\n用户名:"+strUserName+"\n"+"密码:"+strUserPwd+"\n");
}
}
else
{
Toast.makeText(UserRegs.this, "您要注册的用户名已经存在!", Toast.LENGTH_SHORT).show();
}
}
private boolean isExistUserName(String strUserName) {
Cursor cursor = db.rawQuery("select * from tuserinfo where username='"+strUserName+"'",null);
if(cursor.getCount() > 0)
{
cursor.close();
return true;
}
else
{
cursor.close();
return false;
}
}
private boolean isPwdSame(String strUserPwd,String strUserRePwd) {
if(strUserPwd.equals(strUserRePwd))
{
return true;
}
else
{
return false;
}
}
private boolean isStrEmpty(String strInput) {
if(strInput.equals(""))
{
return true;
}
else
{
return false;
}
}
private void OpenCreateDB() {
try
{
db = openOrCreateDatabase(DB_NAME,this.MODE_PRIVATE,null);
}
catch (Throwable e)
{
Log.e("tag","openDatabase error:" + e.getMessage());
db = null;
}
try
{
db.execSQL("CREATE TABLE IF NOT EXISTS tuserinfo (_id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR, userpwd VARCHAR, usertype INTEGER)");
}
catch (SQLException se)
{
String msg = "doInstall.error:[%s].%s";
Log.d("tag",String.format(msg, se.getClass(),se.getMessage()));
}
}
protected void onDestroy()
{
super.onDestroy();
if(db!=null)
{
db.close();
}
}
}
<FrameLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.sqlite.UserRegs"
tools:ignore="MergeRootFrame">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvURTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="欢迎注册" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvURUsername"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="用户名:" />
<EditText
android:id="@+id/etURUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入用户名" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvURPwd"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="密码:" />
<EditText
android:id="@+id/etURPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入密码"
android:inputType="textPassword" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvURRePwd"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="密码确认:" />
<EditText
android:id="@+id/etURRePwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请确认密码"
android:inputType="textPassword" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定" />
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消" />
LinearLayout>
LinearLayout>
FrameLayout>
package com.example.administrator.sqlite;
import android.app.Application;
import android.widget.CheckBox;
import android.widget.TextView;
/**
* Created by Administrator on 2017-11-13.
*/
public class ViewHolder {
public TextView tv = null;
public CheckBox cb = null;
}
package com.example.administrator.sqlite;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Welcome extends AppCompatActivity {
Button btn_pwd = null;
Button btn_Manage = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
final String name = getIntent().getStringExtra("name");
Toast.makeText(Welcome.this, name+" , 欢迎登录!", Toast.LENGTH_SHORT).show();
btn_pwd = ((Button) findViewById(R.id.btn_pwdChange));
btn_pwd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),Change.class);
intent.putExtra("name",name);
startActivity(intent);
}
});
btn_Manage = ((Button) findViewById(R.id.btn_Manage));
btn_Manage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),UserManage.class);
startActivity(intent);
}
});
}
}
<android.support.constraint.ConstraintLayout 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.example.administrator.sqlite.Welcome">
<TextView
android:id="@+id/textView2"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:text="Welcome"
android:textSize="30dp"
android:textStyle="italic"
android:gravity="center"
tools:layout_editor_absoluteX="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.244" />
<Button
android:id="@+id/btn_pwdChange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改密码"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/btn_Manage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看用户"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.709" />
android.support.constraint.ConstraintLayout>
package com.example.administrator.sqlite;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 Change extends AppCompatActivity {
private static final String DB_NAME="dbUser.db";
private SQLiteDatabase db=null;
private Button btnOk=null;
private Button btnCancel=null;
private TextView tvUserName=null;
private EditText etOldPwd=null;
private EditText etNewPwd=null;
private EditText etRePwd=null;
private TextView tvShowInfo=null;
private String strUserName="";
private String strOldPwd="";
private String strNewPwd="";
private String strRePwd="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change);
tvUserName=(TextView)findViewById(R.id.tvPCCurrentUN);
etOldPwd=(EditText)findViewById(R.id.etPCOldPwd);
etNewPwd=(EditText)findViewById(R.id.etPCNewPwd);
etRePwd=(EditText)findViewById(R.id.etPCRePwd);
tvShowInfo=(TextView)findViewById(R.id.tvPCShowInfo);
btnOk=(Button)findViewById(R.id.btnPCOK);
btnCancel=(Button)findViewById(R.id.btnPCCancel);
strUserName = getIntent().getStringExtra("name");
tvUserName.setText(strUserName);
tvUserName.setEnabled(false);
OpenCreateDB();
Button.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.getId() == R.id.btnPCOK)
{
strOldPwd=etOldPwd.getText().toString();
strNewPwd=etNewPwd.getText().toString();
strRePwd=etRePwd.getText().toString();
if(isStrEmpty(strOldPwd) == false)
{
if(isStrEmpty(strNewPwd) == false)
{
if(isStrEmpty(strRePwd) == false)
{
if(isPwdSame(strNewPwd,strRePwd) == true)
{
Toast.makeText(getApplicationContext(), "修改密码成功!", Toast.LENGTH_SHORT).show();
//tvShowInfo.setText("修改密码成功");
updatePwd(strUserName,strNewPwd);
}
else
{
tvShowInfo.setText("新密码和确认密码不一致");
etRePwd.setFocusable(true);
}
}
else
{
tvShowInfo.setText("请确认新密码");
etRePwd.setFocusable(true);
}
}
else
{
tvShowInfo.setText("请输入新密码");
etNewPwd.setFocusable(true);
}
}
else
{
tvShowInfo.setText("请输入原始密码");
etOldPwd.setFocusable(true);
}
}
else
{
etOldPwd.setText("");
etRePwd.setText("");
etNewPwd.setText("");
etOldPwd.setFocusable(true);
}
}
};
btnOk.setOnClickListener(listener);
btnCancel.setOnClickListener(listener);
}
private void OpenCreateDB()
{
try
{
db = openOrCreateDatabase(DB_NAME, this.MODE_PRIVATE, null);
}
catch (Throwable e)
{
Log.e("tag","openDatabase error:" + e.getMessage());
db=null;
}
try
{
db.execSQL("CREATE TABLE IF NOT EXISTS tuserinfo (_id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR, userpwd VARCHAR, usertype INTEGER)");
}
catch (SQLException se)
{
String msg = "doInstall.error:[%s].%s";
Log.d("tag",String.format(msg, se.getClass(),se.getMessage()));
}
}
private boolean isStrEmpty(String strInput)
{
if(strInput.equals(""))
{
return true;
}
else
{
return false;
}
}
private boolean isPwdSame(String strUserPwd,String strUserRePwd){
if(strUserPwd.equals(strUserRePwd))
{
return true;
}
else
{
return false;
}
}
private boolean updatePwd(String strUserName,String strNUserPwd)
{
String sql = "update [tuserinfo] set userpwd = '"+strNUserPwd+"' where username='"+strUserName+"'";
try
{
db.execSQL(sql);
return true;
}
catch (SQLException e)
{
return false;
}
}
protected void onDestroy()
{
super.onDestroy();
if(db!=null)
{
db.close();
}
}
}
<android.support.constraint.ConstraintLayout 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.example.administrator.sqlite.Change">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvPCTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户密码修改" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvPCUserName"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="用户名:" />
<TextView
android:id="@+id/tvPCCurrentUN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="无" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvPCOldPwd"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="原密码:" />
<EditText
android:id="@+id/etPCOldPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPassword">
<requestFocus />
EditText>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvPCNewPwd"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="新密码:" />
<EditText
android:id="@+id/etPCNewPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPassword" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvPCRePwd"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="确认密码:" />
<EditText
android:id="@+id/etPCRePwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPassword" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnPCOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定" />
<Button
android:id="@+id/btnPCCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消" />
LinearLayout>
<TextView
android:id="@+id/tvPCShowInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="信息提示区" />
LinearLayout>
android.support.constraint.ConstraintLayout>
package com.example.administrator.sqlite;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
public class UserManage extends AppCompatActivity {
private static final String DB_NAME = "dbUser.db";
private SQLiteDatabase db = null;
//定义ListView变量lvShowItem
private ListView lvShowItem = null;
private Button btnDelete = null;
private Button btnExit = null;
//提示信息
private TextView tvShowInfo = null;
private int UNLen = -1;
private String strUserName[];
private int iSelectedUser = 0;
private static ArrayList listStr = null;
private static ArrayList listInt = null;
//data
private static List> list = null;
private MyAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_manage);
//找到ListView
lvShowItem=(ListView)findViewById(R.id.lvUMShowItem);
btnDelete=(Button)findViewById(R.id.btnUMDelete);
btnExit=(Button)findViewById(R.id.btnUMExit);
tvShowInfo=(TextView)findViewById(R.id.tvUMShowInfo);
if( getAllNUser() == true)
{
showCheckBoxListView();
}
else
{
tvShowInfo.setText("未找到任何用户");
}
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0 ; i < listStr.size() ; i++)
{
String strTUN = listStr.get(i).toString();
deleteSUN(strTUN);
int position = listInt.get(i).intValue();
list.remove(position);
adapter.syncListInt(i,listStr.size());
adapter.notifyDataSetChanged();
adapter.setCheckBoxValue(false);
}
tvShowInfo.setText("");
Toast.makeText(UserManage.this, "共删除"+iSelectedUser+"个用户", Toast.LENGTH_SHORT).show();
iSelectedUser = 0 ;
listStr.removeAll(listStr);
listInt.removeAll(listInt);
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(UserManage.this,MainActivity.class);
startActivity(intent);
}
});
}
private void deleteSUN(String strUserName)
{
db.execSQL("delete from tuserinfo where username='"+strUserName+"'");
}
public void showCheckBoxListView() {
list = new ArrayList>();
//strUserName数组存放了getAllNUser获得的所有用户名
for(int i = 0 ; i < strUserName.length ; i++)
{
//进入for循环后,每次产生一个item
HashMap map = new HashMap();
//讲一个用户名映射到"item_tv"上
map.put("item_tv",strUserName[i]);
//将false映射"item_cb",这是默认情况
map.put("item_cb",false);
//加载信息至list中
//将由一个用户名和一个布尔类型构成的map加入list中
list.add(map);
//调用MyAdapter的构造函数
adapter = new MyAdapter(this,list,R.layout.aum_listviewitem,
new String[]{"item_tv","item_cb"},new int[]{R.id.item_tv,R.id.item_cb});
//把listview的对象lvShowItem和MyAdapter的对象adapter连起来
//相当于完成了listview和适配器的绑定
lvShowItem.setAdapter(adapter);
listStr = new ArrayList();
listInt = new ArrayList();
//监听listview中的点击事件
lvShowItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
//使用getTag方法获得带holder标志的数据
com.example.administrator.sqlite.ViewHolder holder = ((com.example.administrator.sqlite.ViewHolder) view.getTag());
//在item每次被单击的时候改变CheckBox的状态
holder.cb.toggle();
//同时修改map里面的布尔类型变量的值,同步一下
adapter.isSelected.put(position,holder.cb.isChecked());
//如果CheckBox是被勾上的
if(holder.cb.isChecked() == true)
{
//在listStr中存放被选中的item的用户名
listStr.add(strUserName[position]);
//在listInt中存放被选中的item的位置
listInt.add(Integer.valueOf(position));
}
else//如果CheckBox是没有被勾上
{
//在listStr中除去被选中的item的用户名
listStr.remove(strUserName[position]);
//在listInt中除去被选中的item的位置
listInt.remove(Integer.valueOf(position));
}
//将listStr的用户数量存入iSelectedUser
iSelectedUser=listStr.size();
//在信息提示区提示一下有多少用户被选中了
tvShowInfo.setText("共选中"+iSelectedUser+"个用户");
}
});
}
}
private void OpenCreateDB()
{
try
{
db = openOrCreateDatabase(DB_NAME, this.MODE_PRIVATE, null);
}
catch (Throwable e)
{
Log.e("tag","openDatabase error:" + e.getMessage());
db=null;
}
try
{
db.execSQL("CREATE TABLE IF NOT EXISTS tuserinfo (_id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR, userpwd VARCHAR, usertype INTEGER)");
}
catch (SQLException se)
{
String msg = "doInstall.error:[%s].%s";
Log.d("tag",String.format(msg, se.getClass(),se.getMessage()));
}
}
private boolean getAllNUser() {
//打开数据库,获得db
OpenCreateDB();
//获得普通用户的数量
Cursor cursor = db.rawQuery("select * from tuserinfo where usertype=0",null);
UNLen = cursor.getCount();
if(UNLen >= 0 )
{
//动态申请一个长度为UNLen的String数组strUserName
strUserName = new String[UNLen];
//iLen为strUserName数组的index
int iLen = 0;
if(cursor.moveToFirst())
{
//存入第一个用户的username
strUserName[iLen] = cursor.getString(cursor.getColumnIndex("username"));
iLen++;
while (cursor.moveToNext() && (iLen <= UNLen))
{
//存入接下来几个用户的username
strUserName[iLen] = cursor.getString(cursor.getColumnIndex("username"));
iLen++;
}
cursor.close();
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
protected void onDestroy()
{
super.onDestroy();
if(db!=null)
{
db.close();
}
}
public class MyAdapter extends BaseAdapter {
//定义静态HashMap类型对象,用于记录Listview中的每一项的checkbox的值
public HashMap isSelected;
private Context context;
private LayoutInflater inflater;//用于xml布局文件的实例化
private List> listAdapter = null;
private String KeyString[] = null;//每个item中textview和checkbox的值
private String itemString = null;//每个item中的view值
private int idValue[];
public MyAdapter(Context context, List> list,int resource ,String[] from, int[] to) {
this.context = context;
this.listAdapter = list;
KeyString = new String[from.length];//from.length = 2
idValue = new int[to.length];//to.length = 2
System.arraycopy(from,0,KeyString,0,from.length);//{"item_tv","item_cb"}
System.arraycopy(to,0,idValue,0,to.length);//{R.id.item_tv,R.id.item_cb}
inflater = LayoutInflater.from(context);
setCheckBoxValue(false);
}
private void setCheckBoxValue(boolean bCheck) {
isSelected = new HashMap();
for(int i = 0 ; i @Override
public int getCount() {
return listAdapter.size();
}
@Override
public Object getItem(int position) {
return listAdapter.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
com.example.administrator.sqlite.ViewHolder holder = new com.example.administrator.sqlite.ViewHolder();
if(convertView == null)
{
convertView = LayoutInflater.from(getApplication()).inflate(R.layout.aum_listviewitem,null);
}
holder.tv = ((TextView) convertView.findViewById(R.id.item_tv));
//Toast.makeText(UserManage.this, holder.tv.getText().toString(), Toast.LENGTH_SHORT).show();
holder.cb = ((CheckBox) convertView.findViewById(R.id.item_cb));
convertView.setTag(holder);
//就相当于User user = data.get(position);
HashMap map = listAdapter.get(position);
itemString = map.get(KeyString[0]).toString();//这地方调了一晚上,,,醉了
holder.tv.setText(itemString);
//Toast.makeText(UserManage.this, KeyString[0], Toast.LENGTH_SHORT).show();
holder.cb.setChecked(isSelected.get(position));
return convertView;
}
public void syncListInt(int start, int len)
{
for(int i = start + 1 ; i < len ; i++)
{
listInt.set(i,listInt.get(i)-1);
}
}
}
}
<FrameLayout 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.example.administrator.sqlite.UserManage">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvPCTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户信息管理" />
LinearLayout>
<ListView
android:id="@+id/lvUMShowItem"
android:layout_width="match_parent"
android:layout_height="300dp">
ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnUMDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除" />
<Button
android:id="@+id/btnUMExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="退出" />
LinearLayout>
<TextView
android:id="@+id/tvUMShowInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="信息提示区" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
LinearLayout>
FrameLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_marginTop="30dip">
<ImageView
android:id="@+id/image_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/item_tv"
android:layout_width="230dp"
android:layout_height="60dp"
android:textColor="#000000"
android:textSize="23dp"
android:gravity="center_vertical"
android:layout_margin="2dp"
android:layout_toRightOf="@+id/image_header"
/>
<CheckBox
android:id="@+id/item_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:textColor="#ff0000"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/item_tv"
android:layout_toEndOf="@+id/item_tv"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp" />
RelativeLayout>