基础的增删改查做完了,剩下的就是界面的优化以及一些功能的改进。
package com.example.myapplicationthree; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class MainActivity extends Activity { private static String DB_NAME = "mydb"; private EditText et_leixing; private EditText et_riqi; private EditText et_jine; private EditText et_beizhu; private ArrayList<Map<String, Object>> data; private dbHelper dbHelper; private SQLiteDatabase db; private Cursor cursor; private SimpleAdapter listAdapter; private View view; private ListView listview; private Button selBtn, addBtn, updBtn, delBtn; private Map<String, Object> item; private String selId; private ContentValues selCV; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_leixing = (EditText) findViewById(R.id.et_leixing); et_riqi = (EditText) findViewById(R.id.et_riqi); et_jine = (EditText) findViewById(R.id.et_jine); et_beizhu = (EditText) findViewById(R.id.et_beizhu); listview = (ListView) findViewById(R.id.listView); selBtn = (Button) findViewById(R.id.bt_query); addBtn = (Button) findViewById(R.id.bt_add); updBtn = (Button) findViewById(R.id.bt_modify); delBtn = (Button) findViewById(R.id.bt_del); selBtn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dbFindAll(); } }); addBtn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dbAdd(); dbFindAll(); } }); updBtn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dbUpdate(); dbFindAll(); } }); delBtn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dbDel(); dbFindAll(); } }); dbHelper = new dbHelper(this, DB_NAME, null, 1); db = dbHelper.getWritableDatabase();// 打开数据库 data = new ArrayList<Map<String, Object>>(); dbFindAll(); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { // TODO Auto-generated method stub Map<String, Object> listItem = (Map<String, Object>) listview.getItemAtPosition(position); et_leixing.setText((String) listItem.get("leixing")); et_riqi.setText((String) listItem.get("riqi")); et_jine.setText((String) listItem.get("jine")); et_beizhu.setText((String) listItem.get("beizhu")); selId = (String) listItem.get("_id"); Log.i("mydbDemo", "id=" + selId); } }); } //数据删除 protected void dbDel() { // TODO Auto-generated method stub String where = "_id=" + selId; int i = db.delete(dbHelper.TB_NAME, where, null); if (i > 0) Log.i("myDbDemo", "数据删除成功!"); else Log.i("myDbDemo", "数据未删除!"); } private void showList() { // TODO Auto-generated method stub listAdapter = new SimpleAdapter(this, data, R.layout.listview, new String[]{"_id", "leixing", "riqi","jine","beizhu"}, new int[]{R.id.tvID, R.id.tvLX, R.id.tvRQ, R.id.tvJE, R.id.tvBZ}); listview.setAdapter(listAdapter); } //更新列表中的数据 protected void dbUpdate() { // TODO Auto-generated method stub ContentValues values = new ContentValues(); values.put("leixing", et_leixing.getText().toString().trim()); values.put("riqi", et_riqi.getText().toString().trim()); values.put("jine", et_jine.getText().toString().trim()); values.put("beizhu", et_beizhu.getText().toString().trim()); String where = "_id=" + selId; int i = db.update(dbHelper.TB_NAME, values, where, null); if (i > 0) Log.i("myDbDemo", "数据更新成功!"); else Log.i("myDbDemo", "数据未更新"); } //插入数据 protected void dbAdd() { // TODO Auto-generated method stub ContentValues values = new ContentValues(); values.put("leixing", et_leixing.getText().toString().trim()); values.put("riqi", et_riqi.getText().toString().trim()); values.put("jine", et_jine.getText().toString().trim()); values.put("beizhu", et_beizhu.getText().toString().trim()); long rowid = db.insert(dbHelper.TB_NAME, null, values); if (rowid == -1) Log.i("myDbDemo", "数据插入失败!"); else Log.i("myDbDemo", "数据插入成功!" + rowid); } //查询数据 protected void dbFindAll() { // TODO Auto-generated method stub data.clear(); cursor = db.query(dbHelper.TB_NAME, null, null, null, null, null, "_id ASC"); cursor.moveToFirst(); while (!cursor.isAfterLast()) { String id = cursor.getString(0); String leixing = cursor.getString(1); String jine = cursor.getString(2); String riqi = cursor.getString(3); String beizhu = cursor.getString(4); item = new HashMap<String, Object>(); item.put("_id", id); item.put("leixing", leixing); item.put("jine", jine); item.put("riqi", riqi); item.put("beizhu", beizhu); data.add(item); cursor.moveToNext(); } showList(); } }View Code
package com.example.myapplicationthree; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class dbHelper extends SQLiteOpenHelper { public static final String TB_NAME = "friends"; public dbHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("CREATE TABLE IF NOT EXISTS " + TB_NAME + " ( _id integer primary key autoincrement," + "leixing varchar," + "riqi varcher,"+ "jine integer,"+ "beizhu varcher" + ") "); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS " + TB_NAME); onCreate(db); } }View Code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:addStatesFromChildren="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="类型" android:textColor="?android:attr/textColorSecondary" /> <EditText android:id="@+id/et_leixing" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:addStatesFromChildren="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="日期" android:textColor="?android:attr/textColorSecondary" /> <EditText android:id="@+id/et_riqi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:addStatesFromChildren="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="金额" android:textColor="?android:attr/textColorSecondary" /> <EditText android:id="@+id/et_jine" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:addStatesFromChildren="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="备注" android:textColor="?android:attr/textColorSecondary" /> <EditText android:id="@+id/et_beizhu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:addStatesFromChildren="true" android:gravity="center"> <Button android:id="@+id/bt_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="addbutton" android:text="添加"> </Button> <Button android:id="@+id/bt_modify" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="updatebutton" android:text="修改"> </Button> <Button android:id="@+id/bt_del" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="updatebutton" android:text="删除"> </Button> <Button android:id="@+id/bt_query" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="querybutton" android:text="查询"> </Button> </LinearLayout> <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dip"> </ListView> </LinearLayout>View Code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linear" android:layout_width="fill_parent" android:layout_height="50dp" android:orientation="vertical" android:padding="5dip"> <LinearLayout android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal"> <TextView android:id="@+id/tvID" android:layout_width="80dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvLX" android:layout_width="80dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvRQ" android:layout_width="80dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvJE" android:layout_width="80dp" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal"> <TextView android:id="@+id/tvBZ" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> </LinearLayout>View Code