安卓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;
}
}