首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

《2》Android 数据库 SQlite SQLiteOpenHelper

2012-08-09 
《二》Android 数据库 SQlite SQLiteOpenHelper/*大家都知道写博客会很累的,大热天的。希望转载请注明出处:h

《二》Android 数据库 SQlite SQLiteOpenHelper

/*

大家都知道写博客会很累的,大热天的。

希望=转载请注明出处:http://blog.csdn.net/ta893115871

请不要可怜你的鼠标,(*^__^*) 嘻嘻……

*/

 界面如下所示:

《2》Android 数据库 SQlite SQLiteOpenHelper

有一个问题:就是为什么Menu的图标设置不上啊,现在用的是4.0.4的SDK版本,以前用的是SDK2.1的可以。

一是我在.xml文件中iem中加了属性 android:icon="@drawable/add"不能显示。

二是我在java文件中setion(R.drawable.add);也不可以。

哪位大侠知道,给我留下言,万分感谢啊。

 

SQliteOpenHelperDemo.java

package com.example.sqliteopenhelperdemo;import android.os.Bundle;import android.app.Activity;import android.database.Cursor;import android.database.sqlite.SQLiteCursor;import android.view.KeyEvent;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.EditText;import android.widget.ListView;import android.support.v4.widget.SimpleCursorAdapter;public class SQliteOpenHelperDemo extends Activity {private EditText mEditText;private ListView mListView;private SimpleCursorAdapter adapter;private MyDataBase dataBase;private  Cursor mCursor;private int _id;//当前游标Cursor所在的字段值@SuppressWarnings("deprecation")@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_sqlite_open_helper_demo);mEditText = (EditText) this.findViewById(R.id.edit_id);mListView = (ListView) this.findViewById(R.id.listView_id);mListView.setOnItemClickListener(clickListener);mListView.setOnItemSelectedListener(selectedListener);//创建SQLiteOpenHelper对象的引用dataBase=new MyDataBase(this);    /* 取得DataBase里的资料 */mCursor=dataBase.query();    /* new SimpleCursorAdapter并将myCursor传入,显示数据的字段为   _id,todo_text */ adapter=new SimpleCursorAdapter(this,R.layout.list,mCursor, new String[]{MyDataBase.FIELD_id,MyDataBase.FIELD_TEXT       },        new int[]{R.id.itemid_id,R.id.itemcon_id,});mListView.setAdapter(adapter);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_sqlite_open_helper_demo, menu);return true;}public boolean onMenuItemSelected(int featureId, MenuItem item) {switch (item.getItemId()) {case R.id.menu_add:this.add();break;case R.id.menu_modfi:this.mod();break;case R.id.menu_delete:            this.delete();break;default:break;}return false;};   // ListView的点击事件处理OnItemClickListener clickListener = new OnItemClickListener() {public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stubmCursor.moveToPosition(arg2);_id=mCursor.getInt(mCursor.getColumnIndex(MyDataBase.FIELD_id));mEditText.setText(mCursor.getString(mCursor.getColumnIndex(MyDataBase.FIELD_TEXT)));}};//监听处理鼠标滚轮的事件处理OnItemSelectedListener selectedListener = new OnItemSelectedListener() {public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stub_id=mCursor.getInt(mCursor.getColumnIndex(MyDataBase.FIELD_id));SQLiteCursor  c=(SQLiteCursor)arg0.getSelectedItem();mEditText.setText(c.getString(c.getColumnIndex(MyDataBase.FIELD_TEXT)));}public void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}};//更新ListViewpublic void  myUpDateShow(){         /* 重新查询 */mCursor.requery();//告知适配器已经改变adapter.notifyDataSetChanged();mEditText.setText("");_id = 0; }//增加public void add(){String str=mEditText.getText().toString();if (str.equalsIgnoreCase("")) {return;} /* 新增数据到数据库 */dataBase.addMethod(str);myUpDateShow();}//修改public void mod(){String str=mEditText.getText().toString();if (str.equalsIgnoreCase("")) {return;}//修改dataBase.modMethod(_id,str);myUpDateShow();}//删除public void delete(){if (_id==0) {return;}dataBase.deleteMethod(_id);myUpDateShow();}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stubif (keyCode==KeyEvent.KEYCODE_BACK) {//关闭数据库,退出程序dataBase.close();this.finish();}return super.onKeyDown(keyCode, event);}}

MyDataBase.java

package com.example.sqliteopenhelperdemo;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;public class MyDataBase extends SQLiteOpenHelper {private static final String DATABASE_NAME = "demo_db";//数据库的名字private static final int DATABASEVERSION = 1;//版本号private static final String TABLE_NAME = "demo_table";//表名public static final String FIELD_id = "_id";//必须为_idpublic static final String FIELD_TEXT = "demo_text";private SQLiteDatabase db;//数据库private static final String TAG = "MyDataBase";public MyDataBase(Context context) {super(context, DATABASE_NAME, null, DATABASEVERSION);// TODO Auto-generated constructor stubdb = this.getWritableDatabase();// 打开或新建数据库(第一次时创建)获得SQLiteDatabase对象,为了读取和写入数据}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stub/* 建立表table */Log.i(TAG, " onCreate() ");String sql = "CREATE TABLE " + TABLE_NAME + " (" + FIELD_id+ " INTEGER PRIMARY KEY AUTOINCREMENT," + " " + FIELD_TEXT+ " TEXT)";db.execSQL(sql);}//更新数据库@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stubLog.i(TAG, " onUpgrade() ");//删除表的SQLString sql = "DROP TABLE IF EXITS " + TABLE_NAME;db.execSQL(sql);onCreate(db);}//关闭数据库@Overridepublic synchronized void close() {// TODO Auto-generated method stubLog.i(TAG, " close() ");db.close();super.close();}@Overridepublic void onOpen(SQLiteDatabase db1) {// TODO Auto-generated method stubLog.i(TAG, " onOpen() ");super.onOpen(db1);}// 增加数据public long addMethod(String str) {/* 将新增的值放入ContentValues */ContentValues cv = new ContentValues();cv.put(FIELD_TEXT, str);//键值对long row = db.insert(TABLE_NAME, null, cv);Log.i(TAG, "addMethod row=" + row);return row;}// 删除public void deleteMethod(int id) {String[] whereArgs = { Integer.toString(id) };int rowsaffected = db.delete(TABLE_NAME, FIELD_id + "=?", whereArgs);Log.i(TAG, "deleteMethod() rowsaffected=" + rowsaffected);}// 修改public void modMethod(int id, String str) {ContentValues values = new ContentValues();values.put(FIELD_TEXT, str);String[] whereArgs = { Integer.toString(id) };int rowsaffected = db.update(TABLE_NAME, values, FIELD_id + " = ?",whereArgs);Log.i(TAG, "modMethod() rowsaffected=" + rowsaffected);}// 查询所有的数据 ,返回Cursor对象public Cursor query() {//asc是升序desc为降序(默认为asc)return db.query(TABLE_NAME, null, null, null, null, null,  FIELD_id + " ASC");}}

写不开了
其他布局xml文件等见资源:

http://download.csdn.net/detail/ta893115871/4440305


 

热点排行