大二了,终于开始项目开发了。这个项目是我第一个android小软件搞了一天了。感谢网友 大灰狼叔叔 的提供的源码。
下载地址:http://download.csdn.net/detail/hong0220/4669267
核心代码:
MainActivity.java
package com.feelling.ui; import java.util.ArrayList; import com.woz.util.Config; import com.woz.util.DataBaseHelper; import android.app.Activity; 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; public class MainActivity extends Activity{ Button addButton,queryButton; // 按钮-添加,查询 EditText username,phone; @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) { 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) { System.out.println(" on insert table user ---> "); DataBaseHelper dbHelper = new DataBaseHelper(MainActivity.this,Config.DB_NAME); SQLiteDatabase sdb = dbHelper.getWritableDatabase(); // put value String userStr = username.getText().toString(); String phoneStr = phone.getText().toString(); if(userStr.length()>0 && phoneStr.length()>0){ ContentValues cv = new ContentValues(); 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) { int flag=contacts[which].indexOf("."); String artId = contacts[which].substring(0, flag); alertDeleteOpt(artId); } }) .setNegativeButton("取消", null).show(); } }); } public void showAlert(final String msg){ new AlertDialog.Builder(MainActivity.this) .setMessage(msg) .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); }}).show(); } // 删除数据 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; } }
DataBaseHelper.java
package com.woz.util; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; 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) { 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)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 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)"); } }
Config.java
package com.woz.util; public class Config { public static final int VERSION = 1; public static final String TABLE_NAME = "t_note"; public static final String DB_NAME = "hb_contact.db"; }