安卓Dao三层架构SQLite数据操作
MainActivity类代码如下
public class MainActivity extends AppCompatActivity {
private EditText userName;
private EditText pwd;
private Context context;
private UserInfoManager userInfoManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText) findViewById(R.id.userName);
pwd = (EditText) findViewById(R.id.pwd);
context=this;
userInfoManager = UserInfoManager.getInstance(context);
}
//注册单击事件
public void registBntOnclick(View view) {
showDialog();
}
//登录点击事件
public void loginBntOnclick(View view) {
User us =new User( userName.getText().toString(),pwd.getText().toString());
Log.i("ddd",pwd.getText().toString());
User user =userInfoManager.login(us);
if (user!=null){
Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
Intent intent =new Intent(this,SecondActivity.class);
intent.putExtra("user",user);
startActivity(intent);
}else {
userName.setText(us.getUserName());
Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show();
}
}
//初始化并弹出对话框方法
private void showDialog() {
View view = LayoutInflater.from(this).inflate(R.layout.regist_dialog_layout, null, false);
final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).create();
//数据处理
final EditText RUserName = (EditText) view.findViewById(R.id.R_userName);//用户名
final EditText RPhone = (EditText) view.findViewById(R.id.R_phone);//电话号码
final EditText RPwd = (EditText) view.findViewById(R.id.R_pwd);//密码
Button okBnt = (Button) view.findViewById(R.id.ok_bnt);//确定按钮
Button noBnt = (Button) view.findViewById(R.id.no_bnt);//取消按钮
//确定按钮单击事件
okBnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final User us =new User();
us.setUserName(RUserName.getText().toString());
us.setPhone(RPhone.getText().toString());
us.setSex("女");
us.setPwd(RPwd.getText().toString());
int count =userInfoManager.add(us);
if (count>0){
/* @setIcon 设置对话框图标
* @setTitle 设置对话框标题
* @setMessage 设置对话框消息提示
*/
final AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainActivity.this);
normalDialog.setTitle("提示");
normalDialog.setMessage("注册成功跳转登录界面吗?");
normalDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog1, int which) {
//...To-do
dialog1.dismiss();
dialog.dismiss();
userName.setText(us.getUserName());
pwd.setText(us.getPwd());
}
});
normalDialog.show();
}else {
final AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainActivity.this);
normalDialog.setTitle("提示");
normalDialog.setMessage("注册失败!");
normalDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog1, int which) {
//...To-do
dialog1.dismiss();
}
});
normalDialog.show();
}
}
});
//取消按钮单击事件
noBnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
//此处设置位置窗体大小,我这里设置为了手机屏幕宽度的3/4 注意一定要在show方法调用后再写设置窗口大小的代码,否则不起效果会
dialog.getWindow().setLayout((ScreenUtils.getScreenWidth(this) / 4 * 4), LinearLayout.LayoutParams.WRAP_CONTENT);
}
}
SecondActivity类代码如下
public class SecondActivity extends AppCompatActivity {
private TextView txUserName;//用户名
private TextView txNumber;//电话
private TextView txInterest;//爱好
private TextView sex;//性别
private TextView myself;//个人简介
private Button selAll;//查询所有
private Button upPwd;//修改密码
private User user;//用户对象
private Context context;//上下文
private UserInfoManager userInfoManager;
private ListView lv;//ListView视图
private List<User> dataList = new ArrayList<User>();//存储数据
private ListItemAdapter listViewAdapter;//ListView的数据适配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
context = this;
userInfoManager = UserInfoManager.getInstance(context);
//初始化控键
intin();
}
//初始化控键
private void intin() {
txUserName = (TextView) findViewById(R.id.tx_userName);
txNumber = (TextView) findViewById(R.id.tx_number);
txInterest = (TextView) findViewById(R.id.tx_interest);
sex = (TextView) findViewById(R.id.sex);
myself = (TextView) findViewById(R.id.myself);
selAll = (Button) findViewById(R.id.sel_all);
upPwd = (Button) findViewById(R.id.up_pwd);
lv = (ListView) findViewById(R.id.lv);
Intent intent = getIntent();
user = (User) getIntent().getSerializableExtra("user");
txUserName.setText("用 户 名:"+user.getUserName());
txNumber.setText("电话号码:"+user.getPhone());
Log.i("activity_second", user.getUserName());
}
//修改密码
public void upPwdOnclick(View view) {
showDialog();
}
//查询所有用户
public void selAllOnclick(View view) {
//初始化5项数据
dataList = userInfoManager.findAll();
//设置ListView的适配器
listViewAdapter = new ListItemAdapter(this, dataList);
lv.setAdapter(listViewAdapter);
lv.setSelection(4);
}
//初始化并弹出对话框方法
private void showDialog() {
View view = LayoutInflater.from(this).inflate(R.layout.update_pwd_dialog_layout, null, false);
final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).create();
//数据处理
final EditText UpUserName = (EditText) view.findViewById(R.id.Up_userName);
final EditText oldPwd = (EditText) view.findViewById(R.id.old_pwd);
final EditText newPwd = (EditText) view.findViewById(R.id.new_pwd);
Button upOkBnt = (Button) view.findViewById(R.id.up_ok_bnt);
Button upNoBnt = (Button) view.findViewById(R.id.up_no_bnt);
//确定按钮单击事件
upOkBnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
User us = new User(UpUserName.getText().toString(), oldPwd.getText().toString());
int count = userInfoManager.updatePwd(us, newPwd.getText().toString());
if (count > 0) {
Toast.makeText(SecondActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SecondActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
}
});
//取消按钮单击事件
upNoBnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
//此处设置位置窗体大小,我这里设置为了手机屏幕宽度的3/4 注意一定要在show方法调用后再写设置窗口大小的代码,否则不起效果会
dialog.getWindow().setLayout((ScreenUtils.getScreenWidth(this) / 4 * 4), LinearLayout.LayoutParams.WRAP_CONTENT);
}
public class ListItemAdapter extends BaseAdapter {
private Context context;//上下文对象
private List<User> dataList;//ListView显示的数据
/**
* 构造器
*
* @param context 上下文对象
* @param dataList 数据
*/
public ListItemAdapter(Context context, List<User> dataList) {
this.context = context;
this.dataList = dataList;
}
@Override
public int getCount() {
return dataList == null ? 0 : dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
//判断是否有缓存
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.all_item_layout, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
//得到缓存的布局
viewHolder = (ViewHolder) convertView.getTag();
}
//设置内容
viewHolder.txName.setText(dataList.get(position).getUserName());
viewHolder.delBnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//从集合中删除所删除项的EditText的内容
int count = userInfoManager.remove(dataList.get(position).getId());
if (count > 0) {
dataList.remove(position);
listViewAdapter.notifyDataSetChanged();
Toast.makeText(SecondActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SecondActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
}
}
});
return convertView;
}
/**
* ViewHolder类
*/
class ViewHolder {
TextView txName;
Button delBnt;
/**
* 构造器
*
* @param view 视图组件(ListView的子项视图)
*/
ViewHolder(View view) {
txName = (TextView) view.findViewById(R.id.tx_name);
delBnt = (Button) view.findViewById(R.id.del_bnt);
}
}
}
activity_main.xml 代码块
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/cz_bj"
android:padding="15dp"
tools:context=".avtivity.MainActivity">
<LinearLayout
android:id="@+id/layout_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="265dp"
android:orientation="vertical"
android:padding="15dp">
<EditText
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:background="@drawable/cz_shade_ll"
android:hint="用户名"
android:paddingLeft="10dp"
android:textSize="20dp" />
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:background="@drawable/cz_shade_ll"
android:hint="密 码"
android:inputType="textPassword"
android:paddingLeft="10dp"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_bnt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layout_et"
android:orientation="horizontal"
android:padding="25dp">
<Button
android:id="@+id/login_bnt"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="@drawable/cz_shade_button"
android:text="登录"
android:onClick="loginBntOnclick"
android:textColor="#fff"
android:textSize="20dp" />
<Button
android:id="@+id/regist_bnt"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="@drawable/cz_shade_button"
android:onClick="registBntOnclick"
android:text="注册"
android:textColor="#fff"
android:textSize="20dp" />
</LinearLayout>
</RelativeLayout>
activity_second.xml 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".avtivity.SecondActivity">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="30dp"
android:background="@drawable/cz_head" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/tx_userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="昵 称:"
android:textColor="#000000"
android:textSize="20sp"
android:layout_marginBottom="5dp"/>
<TextView
android:id="@+id/tx_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="电话号码:"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layout1"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/tx_interest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/interest"
style="@style/text_Style"
/>
<TextView
android:id="@+id/sex"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="性别:女"
style="@style/text_Style"
/>
<TextView
android:id="@+id/myself"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/myself"
style="@style/text_Style"
/>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="250dp"></ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/layout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layout2"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp"
android:layout_below="@id/layout3"
>
<Button
android:id="@+id/sel_all"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:text="所有用户"
android:onClick="selAllOnclick"
android:textColor="#000"
android:textSize="18sp"
/>
<Button
android:id="@+id/up_pwd"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:text="修改密码"
android:onClick="upPwdOnclick"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:textSize="18sp"
/>
</LinearLayout>
</RelativeLayout>
ListView标签item 布局
<?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="horizontal"
android:padding="10dp"
>
<TextView
android:id="@+id/tx_name"
android:layout_width="360dp"
android:layout_height="wrap_content"
android:text="张山"
android:textSize="20dp"
android:textColor="#000"
/>
<Button
android:id="@+id/del_bnt"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/del"/>
</LinearLayout>
dialog弹窗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"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册信息"
android:layout_gravity="center"
android:textColor="#000"
android:textSize="30dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#303F9F"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="用 户 名:"
android:textSize="18sp"
android:textColor="#000"/>
<EditText
android:id="@+id/R_userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:height="60dp"
android:layout_marginLeft="5dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="电话号码:"
android:textSize="18sp"
android:textColor="#000"/>
<EditText
android:id="@+id/R_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:height="60dp"
android:layout_marginLeft="5dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="18sp"
android:textColor="#000"/>
<EditText
android:id="@+id/R_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginLeft="5dp"
android:height="60dp"
android:textSize="18sp" />
</LinearLayout>
<RelativeLayout
android:id="@+id/layout_bnt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layout_et"
android:layout_marginLeft="100dp"
android:padding="5dp">
<Button
android:id="@+id/ok_bnt"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="确定"
android:textColor="#8EE5EE"
android:textSize="20dp" />
<Button
android:id="@+id/no_bnt"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:text="取消"
android:background="@android:color/transparent"
android:textColor="#8EE5EE"
android:textSize="20dp" />
</RelativeLayout>
</LinearLayout>
pojo实体类User
public class User implements Serializable {
private int id;//编号
private String userName;//昵称
private String phone;//电话
private String myself;//个人简介
private String sex;//性别
private String pwd;//密码
private String interest;//兴趣
public User() {
}
public User(int id, String userName, String phone, String myself, String sex, String pwd, String interest) {
this.id = id;
this.userName = userName;
this.phone = phone;
this.myself = myself;
this.sex = sex;
this.pwd = pwd;
this.interest = interest;
}
public User(String userName, String phone, String myself, String sex, String pwd, String interest) {
this.userName = userName;
this.phone = phone;
this.myself = myself;
this.sex = sex;
this.pwd = pwd;
this.interest = interest;
}
public User(String userName, String pwd) {
this.userName = userName;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMyself() {
return myself;
}
public void setMyself(String myself) {
this.myself = myself;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getInterest() {
return interest;
}
public void setInterest(String interest) {
this.interest = interest;
}
}
创建数据库MySql类继承SQLiteOpenHelper
public class MySql extends SQLiteOpenHelper {
/**
*
* @param context 上下文
* @param name 数据库名
* @param factory 可选的数据库游标工厂类,当查询(query)被提交时,该对象会被调用来实例化一个游标。默认为null
* @param version 数据库版本号
*/
public MySql(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
public MySql(Context context) {
super(context,Constant.DATABASE_NAME,null,Constant.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建用户表格
db.execSQL("create table userInfo(id integer primary key autoincrement,username varchar(20),phone varchar(20),myself varchar(100),sex varchar(10),pwd varchar(20),interest varchar(50))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
创建接口Dao进行数据库增删查改操作
public interface UserDao {
public int add(User user);
public int remove(int id);
public int updatePwd(User user,String newPwd);
public User findById(int id);
public User login(User user);
public List<User> findByCoreBar(PageBean cp);
public List<User> findAll();
}
使用单列模式创建UserInfoManager类实现UserDao对数据库进行操作
public class UserInfoManager implements UserDao {
private static ContentValues values;
private static SQLiteDatabase db;//创建SQLiteDatabase对象
private static MySql mySql;//数据链接池
private static UserInfoManager userInfoManager = null;
private UserInfoManager(){};//私有构造函数,避免外界可以通过new 获取对象
private UserInfoManager(final Context context) {
//执行一些初始化的任务
new Thread(){
public void run(){
//创建数据库对象
if (mySql == null) {
mySql = new MySql(context);
}
}
}.start();
}
public static synchronized UserInfoManager getInstance(Context context){
if(userInfoManager == null){
userInfoManager = new UserInfoManager(context);
}
return userInfoManager;
}
/**
* @param user 注册
*/
@Override
public int add(User user) {
Log.i("d", user.getUserName());
try {
//获取可读写的SQLiteDatabase对象
db = mySql.getWritableDatabase();
//创建ContentValues对象
values = new ContentValues();
//将数据添加到ContentValues对象中去
values.put("username", user.getUserName());
values.put("phone", user.getPhone());
values.put("myself", user.getMyself());
values.put("sex", user.getSex());
values.put("pwd", user.getPwd());
values.put("interest", user.getInterest());
int index = (int) db.insert("userInfo", null, values);
return index;
} catch (Exception e) {
e.printStackTrace();
} finally {
db.close();
}
return 0;
}
/**
* @param id
*/
@Override
public int remove(int id) {
db=mySql.getWritableDatabase();
int index =db.delete("userInfo","id=?",new String[]{id+""});
db.close();
return index;
}
/**
* @param user
* @return
*/
@Override
public int updatePwd(User user,String newPwd) {
db=mySql.getWritableDatabase();
values =new ContentValues();
values.put("pwd",newPwd);
int index = db.update("userInfo",values,"username=?",new String[]{user.getUserName()});
db.close();
return index;
}
/**
* @param id
* @return
*/
@Override
public User findById(int id) {
db = mySql.getReadableDatabase();
User user = null;
Cursor cursor = db.query("userInfo",null,"id=?",new String[]{id+""},null,null,null);
if(cursor.moveToNext()){
user = new User();
user.setId(cursor.getInt(0));
user.setUserName(cursor.getString(1));
user.setPhone(cursor.getString(2));
user.setMyself(cursor.getString(3));
user.setSex(cursor.getString(4));
user.setPwd(cursor.getString(5));
user.setInterest(cursor.getString(6));
}
db.close();
return user;
}
/**
* @param user 登录
* @return
*/
@Override
public User login(User user) {
db = mySql.getReadableDatabase();
Log.i("ddd",user.getPwd());
User us = null;
Cursor cursor = db.rawQuery("select * from userInfo where username=? and pwd = ?", new String[]{user.getUserName(), user.getPwd()});
/* Cursor cursor = db.query("userInfo",null,"username=? and pwd = ?",new String[]{user.getUserName(), user.getPwd()},null,null,null);*/
Log.i("ddd",user.getUserName());
if (cursor.moveToNext()) {
us = new User();
us.setId(cursor.getInt(0));
us.setUserName(cursor.getString(1));
us.setPhone(cursor.getString(2));
us.setMyself(cursor.getString(3));
us.setSex(cursor.getString(4));
us.setPwd(cursor.getString(5));
us.setInterest(cursor.getString(6));
}
db.close();
return us;
}
/**
* @param cp
* @return
*/
@Override
public List<User> findByCoreBar(PageBean cp) {
db = mySql.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from userInfo limit "+((cp.getThisPage()-1)*cp.getStep())+","+cp.getStep(),null);
List<User> list = new ArrayList<User>();
while(cursor.moveToNext()){
User user = new User();
user.setId(cursor.getInt(0));
user.setUserName(cursor.getString(1));
user.setPhone(cursor.getString(2));
user.setMyself(cursor.getString(3));
user.setSex(cursor.getString(4));
user.setPwd(cursor.getString(5));
user.setInterest(cursor.getString(6));
list.add(user);
}
db.close();
return list;
}
/**
* @return
*/
@Override
public List<User> findAll() {
db = mySql.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from userInfo", null);
List<User> list = new ArrayList<User>();
while (cursor.moveToNext()) {
User user = new User();
user.setId(cursor.getInt(0));
user.setUserName(cursor.getString(1));
user.setPhone(cursor.getString(2));
user.setMyself(cursor.getString(3));
user.setSex(cursor.getString(4));
user.setPwd(cursor.getString(5));
user.setInterest(cursor.getString(6));
list.add(user);
}
db.close();
return list;
}
public static MySql getIntance(Context context) {
if (mySql == null) {
mySql = new MySql(context);
}
return mySql;
}
}