- <p>哥最近做了一个小项目《铁哥们通讯录》,作为练手项目给大家分享一下。</p><p>功能也很简单,一个主界面,然后在主界面弹出一个添加界面,一个信息列表。</p><p>其中只用到了1个activity,1个sqlite帮助类。</p>
- 下面是activity类
- package com.feelling.ui;
- import java.util.ArrayList;
- import com.woz.util.Config;
- import com.woz.util.DataBaseHelper;
- import android.app.AlertDialog;
- import android.content.ContentValues;
- import android.content.DialogInterface;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- /****
- * @production 铁哥们通讯录
- * @company android技术帮--【技术成就梦想】
- * @department 群号:85506951
- * @author 良仔(qq:953486326)
- * {@docRoot 通讯录的启动初始化界面 }
- * @version 1.0
- * @since 2011\9\21
- * @see 欢迎对android感兴趣的童鞋加入android技术帮,共同学习,共同进步!
- * ****/
- public class MainActivity extends ProDialogImp {
- /** Called when the activity is first created. */
- //---------- main ----------------
- Button addButton,queryButton; // 按钮-添加,查询
- //---------- add user ----------------
- EditText username,phone;
- String userStr,phoneStr;
- //----------- list user -----------------------
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // main
- addButton = (Button)findViewById(R.id.addUserBtn);
- queryButton = (Button)findViewById(R.id.listUserBtn);
- // 添加
- addButton.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- // 这里直接跳到添加的ui中去
- // setContentView(R.layout.add_user);
- LayoutInflater inflater = getLayoutInflater();
- View layout = inflater.inflate(R.layout.add_user, (ViewGroup)findViewById(R.id.dialog));
- // 内容元素
- username = (EditText)layout.findViewById(R.id.userNameET);
- phone = (EditText)layout.findViewById(R.id.phoneNumberET);
- new AlertDialog.Builder(MainActivity.this).setTitle("添加联系人").setView(layout)
- .setPositiveButton("确定", new DialogInterface.OnClickListener() { //添加事件
- public void onClick(DialogInterface dialog, int which) {
- // 这里直接跳到添加的ui中去
- System.out.println(" on insert table user ---> ");
- DataBaseHelper dbHelper = new DataBaseHelper(MainActivity.this,Config.DB_NAME);
- SQLiteDatabase sdb = dbHelper.getWritableDatabase();
- // put value
- userStr = username.getText().toString();
- phoneStr = phone.getText().toString();
- if(userStr!=null&&phoneStr!=null){
- ContentValues cv = new ContentValues();
- // cv.put("id", 1);
- cv.put("title", userStr);
- cv.put("content", phoneStr);
- sdb.insert(Config.TABLE_NAME, null, cv);
- showAlert("数据添加成功!");
- }else{
- showAlert("数据数据格式错误,请重新输入!");
- }
- }}).setNegativeButton("取消", null).show();
- }
- });
- // 查询
- queryButton.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- DataBaseHelper dbHelper = new DataBaseHelper(MainActivity.this,Config.DB_NAME);
- SQLiteDatabase db = dbHelper.getReadableDatabase();
- Cursor cs = db.query(Config.TABLE_NAME, new String[]{"_id","title","content"}, null, null, null, null, null);
- ArrayList<String> alist = new ArrayList<String>();
- while(cs.moveToNext()){
- int id = cs.getInt(cs.getColumnIndex("_id"));
- String name = cs.getString(cs.getColumnIndex("title"));
- String con = cs.getString(cs.getColumnIndex("content"));
- alist.add(id+"."+name+"["+con+"]");
- System.out.println(" query [t_note]--> id = "+ id +" name = " + name +" con = " + con );
- }
- final String[] contacts = new String[alist.size()];
- for(int a=0;a<alist.size();a++){
- contacts[a] = alist.get(a);
- }
- new AlertDialog.Builder(MainActivity.this).setTitle("内容列表").setIcon(
- android.R.drawable.ic_dialog_info).setItems(
- contacts,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- //dialog.dismiss();
- String artId = contacts[which].substring(0, 1);
- alertDeleteOpt(artId);
- // showAlert("msg --> " + artId);
- }
- })
- .setNegativeButton("取消", null).show();
- /* ListAdapter listapt = new SimpleCursorAdapter(MainActivity.this,
- android.R.layout.simple_expandable_list_item_1,
- cs,
- new String[]{"title"},
- new int[]{android.R.id.text1} );
- ListView listUser = new ListView(MainActivity.this);
- listUser.setAdapter(listapt);
- setContentView(listUser); */
- }
- });
- }
- // 删除数据
- public void deleteData(String id){
- DataBaseHelper dbHelper = new DataBaseHelper(MainActivity.this,Config.DB_NAME);
- SQLiteDatabase sdb = dbHelper.getWritableDatabase();
- sdb.delete(Config.TABLE_NAME, " _id=?", new String[]{id}); //删除数据
- }
- /**
- * 删除操作
- */
- public void alertDeleteOpt(final String id){
- new AlertDialog.Builder(this)
- .setCancelable(false)
- .setMessage("您确定要删除这条信息吗?")
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- deleteData(id);
- }})
- .setNegativeButton("取消", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- }})
- .show();
- }
- //退出程序
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK)// 返回按键
- {
- new AlertDialog.Builder(this)
- .setCancelable(false)
- .setIcon(android.R.drawable.btn_star)
- .setTitle("退出")
- .setMessage("您确认要退出程序吗?")
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- MainActivity.this.finish(); //关闭程序的核心方法
- }})
- .setNegativeButton("取消", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- }})
- .show();
- }
- return true;
- }
- }
- 下面sqlite帮助类,其他的不太重要,所以就放到附件了,欢迎大家下载共同讨论学习。
- <p><span style="background-color: #fafafa;">package com.woz.util;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- /****
- * @production 铁哥们通讯录
- * @company android技术帮--【技术成就梦想】
- * @department 群号:85506951
- * @author 良仔(qq:953486326)
- * <a href="mailto:%7B@docRoot">{@docRoot</a> 访问sqlite数据的助手类
- * 创建了一个note记录表
- *
- * _id, 主键
- * title, 标题
- * content,内容 }
- * @version 1.0
- * @since 2011\9\21
- * @see 欢迎对android感兴趣的童鞋加入android技术帮,共同学习,共同进步!
- * ****/
- public class DataBaseHelper extends SQLiteOpenHelper {
- public DataBaseHelper(Context context, String name, CursorFactory factory,
- int version) {
- super(context, name, factory, version);
- // TODO Auto-generated constructor stub
- }
- public DataBaseHelper(Context context, String name,
- int version) {
- this(context, name, null, version);
- // TODO Auto-generated constructor stub
- }
- public DataBaseHelper(Context context, String name) {
- this(context, name, Config.VERSION);
- // TODO Auto-generated constructor stub
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- // TODO Auto-generated method stub
- System.out.println(" create table --> ");
- // 执行创建
- db.execSQL("create table "+ Config.TABLE_NAME +
- "(_id INTEGER DEFAULT '1' NOT NULL PRIMARY KEY AUTOINCREMENT," +
- "title varchar(100)," +
- "content text)");
- }</span></p><p><span style="background-color: #fafafa;"> @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // TODO Auto-generated method stub
- System.out.println(" up grade datebase --> ");
- db.execSQL("drop table " + Config.TABLE_NAME);
- db.execSQL("create table "+ Config.TABLE_NAME +
- "(_id INTEGER DEFAULT '1' NOT NULL PRIMARY KEY AUTOINCREMENT," +
- "title varchar(100)," +
- "content text)");
- }
- }
- </span></p>