基础的增删改查做完了,剩下的就是界面的优化以及一些功能的改进。
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
总结
以上是内存溢出为你收集整理的冲刺全部内容,希望文章能够帮你解决冲刺所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)