Android 在Activity中对SQLite的操作
注册
package com.scme.ui; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Toast; import com.scme.bean.Students; import com.scme.db.DBHelper; public class RegActivity extends Activity implements OnClickListener { private EditText et_Name, et_Pwd, et_Age; private Button bt_Save, bt_Clear; private DBHelper dao; private RadioGroup rg_sex; private RadioButton rg_nan, rg_nv; private String sex = ""; private void init() { et_Name = (EditText) findViewById(R.id.et_Name); et_Pwd = (EditText) findViewById(R.id.et_Pwd); et_Age = (EditText) findViewById(R.id.et_Age); bt_Save = (Button) findViewById(R.id.bt_Save); bt_Clear = (Button) findViewById(R.id.bt_Clear); rg_sex = (RadioGroup) findViewById(R.id.rg_sex); rg_nan = (RadioButton) findViewById(R.id.rg_nan); rg_nv = (RadioButton) findViewById(R.id.rg_nv); dao = new DBHelper(this, null, null, 1); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.reg_activity); init(); bt_Save.setOnClickListener(this); bt_Clear.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_Save: String name = et_Name.getText().toString().trim(); String pwd = et_Pwd.getText().toString().trim(); rg_sex.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup arg0, int arg1) { if (arg1 == R.id.rg_nan) { sex = "男"; } if(arg1 == R.id.rg_nv){ sex = "女"; } } }); int age = Integer.parseInt(et_Age.getText().toString().trim()); if (name.equals("")) { Toast.makeText(RegActivity.this, "姓名不能为空", 1000).show(); return; } if (pwd.equals("")) { Toast.makeText(RegActivity.this, "密码不能为空", 1000).show(); return; } if (sex.equals("")) { Toast.makeText(RegActivity.this, "性别不能为空", 1000).show(); return; } //添加联系人 Students stu = new Students(); stu.setStuName(name); stu.setStuPwd(pwd); stu.setStuSex(sex); stu.setStuAge(age); dao.addStu(stu); Toast.makeText(RegActivity.this, "添加联系人成功", 1000).show(); Intent intent = new Intent(RegActivity.this, LoginActivity.class); startActivity(intent); finish(); break; case R.id.bt_Clear: // 取消注册 finish(); break; } } }
添加
package com.scme.ui; import com.scme.bean.Tongxunlu; import com.scme.db.DBHelper; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class AddActivity extends Activity implements OnClickListener { private EditText add_txl_etName,add_txl_etPhone,add_txl_etEmail,add_txl_etAddr; private Button add_txl_btSave,add_txl_btClear; private DBHelper dao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_activity); dao = new DBHelper(this, null, null, 1); add_txl_etName = (EditText) findViewById(R.id.add_txl_etName); add_txl_etPhone = (EditText) findViewById(R.id.add_txl_etPhone); add_txl_etEmail = (EditText) findViewById(R.id.add_txl_etEmail); add_txl_etAddr = (EditText) findViewById(R.id.add_txl_etAddr); add_txl_btSave = (Button) findViewById(R.id.add_txl_btSave); add_txl_btClear = (Button) findViewById(R.id.add_txl_btClear); add_txl_btSave.setOnClickListener(this); add_txl_btClear.setOnClickListener(this); } @Override public void onClick(View v) { String name = add_txl_etName.getText().toString().trim(); String phone = add_txl_etPhone.getText().toString().trim(); String email = add_txl_etEmail.getText().toString().trim(); String addr = add_txl_etAddr.getText().toString().trim(); if(v.getId() == R.id.add_txl_btClear){ finish(); }else if(v.getId() == R.id.add_txl_btSave){ if(name.equals("")){ Toast.makeText(AddActivity.this, "联系人姓名不能为空", 1000).show(); return ; }if(phone.equals("")){ Toast.makeText(AddActivity.this, "电话号码不能为空", 1000).show(); return ; } //添加联系人 Tongxunlu txl = new Tongxunlu(); txl.setTxlName(name); txl.setTxlPhone(phone); txl.setTxlEmail(email); txl.setTxlAddr(addr); dao.addTxl(txl); Toast.makeText(AddActivity.this, "添加联系人成功", 1000).show(); Intent intent = new Intent(AddActivity.this, MainActivity.class); startActivity(intent); finish(); } } }
登录
package com.scme.ui; import com.scme.db.DBHelper; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class LoginActivity extends Activity implements OnClickListener { private EditText et_Name, et_Pwd; private Button bt_login, bt_Reg; private DBHelper dao; private void init() { et_Name = (EditText) findViewById(R.id.et_Name); et_Pwd = (EditText) findViewById(R.id.et_Pwd); bt_login = (Button) findViewById(R.id.bt_login); bt_Reg = (Button) findViewById(R.id.bt_Reg); dao = new DBHelper(this, null, null, 1); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login_activity); init(); bt_login.setOnClickListener(this); bt_Reg.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_login: String name = et_Name.getText().toString().trim(); String pwd = et_Pwd.getText().toString().trim(); boolean a = dao.login(name, pwd); if (a) { Toast.makeText(LoginActivity.this, "登陆成功", 1000).show(); Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); } else { Toast.makeText(LoginActivity.this, "登陆失败", 1000).show(); } break; case R.id.bt_Reg: Intent intent = new Intent(LoginActivity.this, RegActivity.class); startActivity(intent); break; } } }
显示
package com.scme.ui; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import com.scme.bean.Tongxunlu; import com.scme.db.DBHelper; public class MainActivity extends Activity { private EditText et_query; private MyAdapter a; private DBHelper dao; private ListView lv_info; private List<Tongxunlu> lists; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); // 获得显示页面的查询文本框 et_query = (EditText) findViewById(R.id.et_query); // 获得ListView的id lv_info = (ListView) findViewById(R.id.lvTXL); // 实例化DBHelper dao = new DBHelper(this, null, null, 0); // 调用查询方法 lists = dao.queryName(null); // 实例化适配器 a = new MyAdapter(); // 将适配器放入ListView中 lv_info.setAdapter(a); // 当在查询文本框中输入数据是触发查询事件 et_query.addTextChangedListener(new TextWatcher() { // 内容改变后触发 public void onTextChanged(CharSequence s, int start, int before, int count) { lists = dao.queryName(s.toString()); // 当ListView中的数据改变后让适配器自动更新 a = new MyAdapter(); // 将适配器放入ListView中 lv_info.setAdapter(a); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void afterTextChanged(Editable s) { } }); } /** * 返回页面重新查询 */ protected void onRestart() { super.onRestart(); lists = dao.queryName(null); a = new MyAdapter(); // 将适配器放入ListView中 lv_info.setAdapter(a); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(1, 1, 1, "新增联系人"); menu.add(1, 2, 2, "退出"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // 判断点击的菜单的ID if (item.getItemId() == 1) { Intent intent = new Intent(MainActivity.this, AddActivity.class); // startActivity(intent); startActivityForResult(intent, 1); } else { finish(); } return super.onOptionsItemSelected(item); } public class MyAdapter extends BaseAdapter { public int getCount() { return lists.size(); } public Object getItem(int arg0) { return null; } public long getItemId(int arg0) { return 0; } public View getView(int i, View view, ViewGroup arg2) { View v = null; ViewHolder vh; final Tongxunlu txl = lists.get(i); if (view == null) { // 说明当前这一行不是重用的 // 加载行布局文件,产生具体的一行 v = getLayoutInflater().inflate(R.layout.list_view, null); // 创建存储一行控件的对象 vh = new ViewHolder(); vh.name = (TextView) v.findViewById(R.id.te_lx_name); vh.phone = (TextView) v.findViewById(R.id.te_lx_phone); vh.update = (Button) v.findViewById(R.id.list_item_btnUpdate); vh.del = (Button) v.findViewById(R.id.list_item_btnDel); v.setTag(vh); vh.del.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { dao.delTxl(txl.get_id()); lists = dao.queryName(null); a = new MyAdapter(); // 将适配器放入ListView中 lv_info.setAdapter(a); } }); } else { v = view; // 取出隐藏在行中的Tag--取出隐藏在这一行中的vh控件缓存对象 vh = (ViewHolder) view.getTag(); } // 从ViewHolder缓存的控件中改变控件的值 // 这里主要是避免多次强制转化目标对象而造成的资源浪费 vh.name.setText(txl.getTxlName()); vh.phone.setText(txl.getTxlPhone()); return v; } // 存储一行中的控件(缓存作用)---避免多次强转每行的控件 class ViewHolder { TextView name; TextView phone; Button update; Button del; } } }
ListView的优化
public class MyAdapter extends BaseAdapter { public int getCount() { return lists.size(); } public Object getItem(int arg0) { return null; } public long getItemId(int arg0) { return 0; } public View getView(int i, View view, ViewGroup arg2) { View v = null; ViewHolder vh; final Tongxunlu txl = lists.get(i); if (view == null) { // 说明当前这一行不是重用的 // 加载行布局文件,产生具体的一行 v = getLayoutInflater().inflate(R.layout.list_view, null); // 创建存储一行控件的对象 vh = new ViewHolder(); vh.name = (TextView) v.findViewById(R.id.te_lx_name); vh.phone = (TextView) v.findViewById(R.id.te_lx_phone); vh.update = (Button) v.findViewById(R.id.list_item_btnUpdate); vh.del = (Button) v.findViewById(R.id.list_item_btnDel); v.setTag(vh); vh.del.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { dao.delTxl(txl.get_id()); lists = dao.queryName(null); a = new MyAdapter(); // 将适配器放入ListView中 lv_info.setAdapter(a); } }); } else { v = view; // 取出隐藏在行中的Tag--取出隐藏在这一行中的vh控件缓存对象 vh = (ViewHolder) view.getTag(); } // 从ViewHolder缓存的控件中改变控件的值 // 这里主要是避免多次强制转化目标对象而造成的资源浪费 vh.name.setText(txl.getTxlName()); vh.phone.setText(txl.getTxlPhone()); return v; } // 存储一行中的控件(缓存作用)---避免多次强转每行的控件 class ViewHolder { TextView name; TextView phone; Button update; Button del; } }