//下面有详细的注释,
package com.ldci.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBdao{
private static final String DB_NAME="info.db";
private static final String TB_NAME="email_tab";
//以下声明的是存放在数据库中列名
static final String F_ID="_id";
static final String F_FROMEMAIL="fromEmail";
static final String F_PASSWORD="password";
static final String F_TOEMAIL="toEmail";
private SQLiteDatabase db;
private DbHelper dbHelper;
private Context context;
public DBdao(Context context){
this.context=context;
}
public void open(){
dbHelper=new DbHelper(context);
db=dbHelper.getWritableDatabase(); //获得一个可写的数据库对象 ----这是创建数据库最常用的方法
}
public void close(){
db.close();
}
//插入
public long addInfo(String from,String pass,String to){
ContentValues initValues=new ContentValues();
initValues.put(F_FROMEMAIL, from);
initValues.put(F_PASSWORD,pass);
initValues.put(F_TOEMAIL,to);
return db.insert(TB_NAME, null, initValues);
}
//删除一条数据(只删除选中的一行) --是通过id来删除
public long delete(long rowId){
return db.delete(TB_NAME, F_ID+"="+rowId,null);
}
//清空(删除所有行)
public long deleteAll(){
return db.delete(TB_NAME, null,null);
}
//更新 --是通过Id来执行更新操作
public long updata(long rowId,String from,String pass,String to ){
ContentValues values=new ContentValues();
values.put(F_FROMEMAIL, from);
values.put(F_PASSWORD, pass);
values.put(F_TOEMAIL, to);
return db.update(TB_NAME, values, F_ID+"="+rowId, null);
}
/**
* 查询记录
* @return
*/
//查询所有数据
public Cursor getAll(){
String sql="select * from email_tab";
return db.rawQuery(sql,null); //将返回一个Cursor对象,Cursor对象包含查询到的所有数据
}
//查询一条数据 (通过Id)
public Cursor getData(long rowId){
return db.rawQuery("select * from email_tab where " + F_ID +"= " + rowId , null);
}
//用while读出Cursor中所有内容
public void readCursor(Cursor cursor){
while(cursor.moveToNext()){
int rowId=cursor.getInt(cursor.getColumnIndex(F_ID));
String from=cursor.getString(cursor.getColumnIndex(F_FROMEMAIL));
String pass=cursor.getString(cursor.getColumnIndex(F_PASSWORD));
String to=cursor.getString(cursor.getColumnIndex(F_TOEMAIL));
//Log.d("aa",rowId+from+pass+to);
}
}
//1. 创建内部类,初始化数据库及数据表
class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context,DB_NAME, null,2);
}
// 在数据库被创建时自动调用onCreate()方法 如果数据库文件已存在,则是不会调用onCreate()方法的
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub //此方法创建了一个数据库表
String sql="create table "+TB_NAME+
"("
+F_ID+" integer primary key AUTOINCREMENT,"
+F_FROMEMAIL+" text,"
+F_PASSWORD+" text,"
+F_TOEMAIL+" text)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists "+TB_NAME);
onCreate(db);
}
}
}
package com.ldci.db;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import com.ldci.smsmessagedemo.R;
public class ShowInfoAct extends Activity implements OnItemClickListener {
private TextView tvNull1;
// 声明数据库访问对象
private DBdao dao = null;
private Cursor cursor = null;
private long listId = 0;
private SimpleCursorAdapter simpleCursorAdapter;
ListView listView= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.showinfo); // 首先加载的是含有ListView组件的布局文件
//先取得代表ListView控件的对象
listView = (ListView) findViewById(R.id.list);
dao = new DBdao(ShowInfoAct.this);
dao.open(); //打开数据库
readerListView();
// if(listView.getCount()==0){
// tvNull1=(TextView)findViewById(R.id.tvNull);
// tvNull1.setText("NO Data...........");
// }
// registerForContextMenu(list); // 为视图注册上下文菜单
}
//用simpleCursorAdapter对象将cursor中的数据显示在ListView上
private void readerListView() {
cursor = dao.getAll();
if(cursor.getCount()==0){ //首先判断是在没有Cursor数据时,将会出现提示信息"No data...."
tvNull1=(TextView)findViewById(R.id.tvNull);
tvNull1.setText("NO Data...........");
}
String [] key={dao.F_FROMEMAIL,dao.F_PASSWORD,dao.F_TOEMAIL}; //数据库的列名
int[] values={R.id.tvFromemail,R.id.tvPassword,R.id.tvToemail}; //对应在数据库每一列中该显示的内容--都是XML中的TextView的Id
simpleCursorAdapter = new SimpleCursorAdapter(ShowInfoAct.this,R.layout.showinfoself, cursor, key,values);
listView.setAdapter(simpleCursorAdapter);
listView.setOnItemClickListener(this);
}
//listView的点击事件 (position标识Item的位置)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// TODO Auto-generated method stub
cursor.moveToPosition(position);
final String itemId = cursor.getString(cursor.getColumnIndex(dao.F_ID));
final String from = cursor.getString(cursor.getColumnIndex(dao.F_FROMEMAIL));
final String pass = cursor.getString(cursor.getColumnIndex(dao.F_PASSWORD));
final String to = cursor.getString(cursor.getColumnIndex(dao.F_TOEMAIL));
// Log.d("aa",itemId);
// Log.d("aa",from);
String[] s = { "修改", "删除", "查看","清空"};
//调用AlertDialog的内部类Builder类来创建AlertDialog对象,,,之后通过Builder类show()方法显示对话框
new AlertDialog.Builder(ShowInfoAct.this).setItems(s,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch (which) {
case 0: //执行修改
Intent intent1 =new Intent(ShowInfoAct.this,UpdateEmailAct.class);
Bundle bundle1=new Bundle();
bundle1.putString("itemId",itemId);
bundle1.putString("from", from);
bundle1.putString("pass", pass);
bundle1.putString("to", to);
intent1.putExtras(bundle1);
startActivity(intent1);
break;
case 1: //删除
dao.delete(Integer.parseInt(itemId)); // 数据库对象调用删除方法
readerListView();
break;
case 2: //查看
Intent intent2=new Intent(ShowInfoAct.this,UpdateEmailAct.class);
Bundle bundle2=new Bundle();
bundle2.putString("itemId", itemId);
bundle2.putString("from",from);
bundle2.putString("pass", pass);
bundle2.putString("to",to);
intent2.putExtras(bundle2);
startActivity(intent2);
break;
case 3: //清空
dao.deleteAll();
readerListView();
break;
}
}
}).show();
}
}
package com.ldci.db;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import com.ldci.smsmessagedemo.R;
import com.ldci.smsmessagedemo.StoreActivity;
public class UpdateEmailAct extends Activity {
private Button butEmail2;
private EditText editEmail,editPass,editTarget;
public static String emailStr,passStr,targetStr;
public String fromStrPre,passPre,toStrPre;
public static String fromStrPreStatic,passPreStatic,toStrPre2Static;
private CheckBox cbSave;
private ProgressDialog myDialog=null;
private DBdao dao;
String id,from,pass,to;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.valemail);
init();
dao=new DBdao(this);
dao.open(); //打开一个数据库
onClickBut();
changeCheck(); //将会调用checkBox的监听事件,将判断checkbooks是否改变
Intent intent=getIntent(); //接收到ShowInfoAct界面传来的数据
Bundle bundle=intent.getExtras();
id=bundle.getString("itemId");
from=bundle.getString("from");
pass=bundle.getString("pass");
to=bundle.getString("to");
// Log.d("aa", from);
editEmail.setText(from);
editPass.setText(pass);
editTarget.setText(to);
StoreActivity.getStoreActivity().addActivity(this);
}
public void onResume(){
super.onResume();
cbSave.setChecked(true);
// load();
changeCheck(); //将会调用checkBox的监听事件,将判断checkbox是否改变
}
//通过id取得代表控件的对象
public void init(){
editEmail=(EditText)findViewById(R.id.editEmail1);
editPass=(EditText)findViewById(R.id.editPass1);
editTarget=(EditText)findViewById(R.id.editTarget);
butEmail2=(Button) findViewById(R.id.go);
cbSave=(CheckBox)findViewById(R.id.checkBoxSave1);
}
public void onClickBut(){ //为"确定"按钮绑定监听事件
butEmail2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(cbSave.isChecked()){ //被选中则是True
save();
Log.d("aa","cb is Checked.....");
}
//显示Progress对话框
myDialog=ProgressDialog.show(UpdateEmailAct.this, "正在验证信息", "请稍后........");
//取得用户输入的信息
emailStr=editEmail.getText().toString().trim(); //将用户输入的email转化为字符串并且去掉前后空格
passStr=editPass.getText().toString().trim(); //取得密码
targetStr=editTarget.getText().toString().trim();
new Thread(){
public void run(){
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}finally{
//卸载所创建的myDialog对象
myDialog.dismiss();
Message msg=handler.obtainMessage();
msg.arg1=1;
handler.sendMessage(msg);
}
}
}.start();
}
});
}
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Toast.makeText(UpdateEmailAct.this,"\n----Email----\n"+emailStr+"\n--- Password----\n"+passStr,Toast.LENGTH_LONG).show();
Intent intent=UpdateEmailAct.this.getIntent();
Bundle bundle=new Bundle();
bundle.putString("email", emailStr);
intent.putExtras(bundle);
Toast.makeText(UpdateEmailAct.this,"您的Email及password相匹配,开启了自动备份功能...",Toast.LENGTH_SHORT).show();
UpdateEmailAct.this.setResult(RESULT_OK,intent );
UpdateEmailAct.this.finish();
}
};
public void changeCheck(){
cbSave.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
}
});
}
public static String fileName="valContent";
private int mode=Activity.MODE_PRIVATE;
public void save(){ //保存数据
SharedPreferences settings=getSharedPreferences(fileName,mode);
SharedPreferences.Editor ed=settings.edit();
ed.putString("fromEmail", editEmail.getText().toString().trim());
ed.putString("password", editPass.getText().toString().trim());
ed.putString("toEmail",editTarget.getText().toString().trim());
ed.commit(); //保存key-value对
}
public void load(){ //读取数据
SharedPreferences settings=getSharedPreferences(fileName, mode);
fromStrPre=settings.getString("fromEmail","******@163.com");
passPre=settings.getString("password","********");
toStrPre=settings.getString("toEmail","****@126.com");
fromStrPreStatic=fromStrPre;
passPreStatic=passPre;
toStrPre2Static=toStrPre;
editEmail.setText(fromStrPre); //将得到的值放到EditText中 ,显示出来
editPass.setText(passPre);
editTarget.setText(toStrPre);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(1,1,1,"收藏").setIcon(R.drawable.save);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case 1:
emailStr=editEmail.getText().toString().trim(); //将用户输入的email转化为字符串并且去掉前后空格
passStr=editPass.getText().toString().trim(); //取得密码
targetStr=editTarget.getText().toString().trim();
Long count=dao.updata(Integer.parseInt(id), emailStr, passStr, targetStr); //调用dao中的更新操作
if(count>0){
Intent intent=new Intent(UpdateEmailAct.this,ShowInfoAct.class);
startActivity(intent);
Toast.makeText(this,"保存成功", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,"保存失败", Toast.LENGTH_LONG).show();
}
// dao.updata(id, editEmail.getText().toString(), editPass.getText().toString(), editTarget.getText().toString());
// Long id=dao.addInfo("FromEmail:"+emailStr, "Password:"+passStr, "ToEmail:"+targetStr);
// if(id>0){
// Toast.makeText(this,"保存成功", Toast.LENGTH_LONG).show();
// }else{
// Toast.makeText(this,"保存失败", Toast.LENGTH_LONG).show();
// }
// Intent intent=new Intent(this,ShowInfoAct.class);
// startActivity(intent);
dao.close(); //关闭数据库对象
this.finish(); //关闭当前的Act
break;
}
return super.onOptionsItemSelected(item);
}
}