Android——SQLite笔记本
package com.example.notebook; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class ToDoDB extends SQLiteOpenHelper { private final static String DATABASE_NAME = "todo_db"; private final static int DATABASE_VERSION = 1; private final static String TABLE_NAME = "todo_table"; public final static String FIELD_id = "_id"; public final static String FIELD_TEXT = "todo_text"; public ToDoDB(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub String sql = "CREATE TABLE " + TABLE_NAME + " (" + FIELD_id + " INTEGER primary key autoincrement, " + " " + FIELD_TEXT + " text)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub String sql = "DROP TABLE IF EXISTS " + TABLE_NAME; db.execSQL(sql); onCreate(db); } public Cursor select() { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null); return cursor; } public long insert(String text) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put(FIELD_TEXT, text); long row = db.insert(TABLE_NAME, null, cv); return row; } public void delete(int id) { SQLiteDatabase db = this.getWritableDatabase(); String where = FIELD_id + " = ?"; String[] whereValue = { Integer.toString(id) }; db.delete(TABLE_NAME, where, whereValue); } public void update(int id, String text) { SQLiteDatabase db = this.getWritableDatabase(); String where = FIELD_id + " = ?"; String[] whereValue = { Integer.toString(id) }; ContentValues cv = new ContentValues(); cv.put(FIELD_TEXT, text); db.update(TABLE_NAME, cv, where, whereValue); } }
package com.example.notebook; import cn.waps.AdView; import cn.waps.AppConnect; import cn.waps.MiniAdView; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.database.Cursor; import android.database.sqlite.SQLiteCursor; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; import android.media.MediaPlayer; public class MainActivity extends Activity { private ToDoDB myToDoDB; private Cursor myCursor; private ListView myListView; private EditText myEditText; private MediaPlayer sound,sound1; private TextView showmore; private int _id; protected final static int MENU_ADD = Menu.FIRST; protected final static int MENU_EDIT = Menu.FIRST + 1; protected final static int MENU_DELETE = Menu.FIRST + 2; @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub super.onOptionsItemSelected(item); switch(item.getItemId()){ case MENU_ADD: this.addTodo(); break; case MENU_EDIT: this.editTodo(); break; case MENU_DELETE: this.deleteTodo(); break; } return super.onOptionsItemSelected(item); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.qq); setTitle("微型记事本"); //06e8abfabdbcb8f90a16d24e54fe5653 //AppConnect.getInstance("06e8abfabdbcb8f90a16d24e54fe5653", "mmw", this); AppConnect.getInstance(this); AppConnect.getInstance(this).setCrashReport(false); myListView = (ListView) this.findViewById(R.id.myListView); myEditText = (EditText) this.findViewById(R.id.myEditText); showmore = (TextView)findViewById(R.id.showmore); // 初始化自定义广告数据 AppConnect.getInstance(this).initAdInfo(); // 初始化插屏广告数据 AppConnect.getInstance(this).initPopAd(this); // 迷你广告调用方式 //定时的更新,点击后进入链接页面 送金币哦 // AppConnect.getInstance(this).setAdBackColor(Color.argb(50, 120, 240, 120));//设置迷你广告背景颜色 // AppConnect.getInstance(this).setAdForeColor(Color.YELLOW);//设置迷你广告文字颜色 LinearLayout miniLayout = (LinearLayout) findViewById(R.id.miniAdLinearLayout); new MiniAdView(this, miniLayout).DisplayAd(15);// 10秒刷新一次 LinearLayout miniLayout2 = (LinearLayout) findViewById(R.id.miniAdLinearLayout2); new MiniAdView(this, miniLayout2).DisplayAd(15);// 10秒刷新一次 // 互动广告调用方式 在窗口固定位置显示一条广告 点击然后就直接下载了 LinearLayout container = (LinearLayout) findViewById(R.id.AdLinearLayout); new AdView(this, container).DisplayAd(); //显示插屏广告 窗口一样跳出来的那种 有个叉叉点了就关闭了 有时候第一次打开没有 第二次打开才会出现 //也许是还没有下载 到缓存中,没有准备好 //判断插屏广告是否已初始化完成,用于确定是否能成功调用插屏广告 boolean hasPopAd = AppConnect.getInstance(this).hasPopAd(this); if(hasPopAd){ AppConnect.getInstance(this).showPopAd(this); //根据指定的theme样式展示插屏广告,theme主要为系统样式id //AppConnect.getInstance(this).showPopAd(this, android.R.style.Theme_Translucent); } sound = new MediaPlayer(); sound = MediaPlayer.create(this,R.raw.welcome111); sound1 = new MediaPlayer(); sound1 = MediaPlayer.create(this,R.raw.welcome121); myToDoDB = new ToDoDB(this); sound.start(); /*取得DataBase里的数据*/ myCursor = myToDoDB.select(); /* new SimpleCursorAdapter,并传入myCursor.显示的字段为todo_text */ SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.mylist, myCursor, new String[] { ToDoDB.FIELD_TEXT }, new int[] { R.id.myTextView1 }); myListView.setAdapter(adapter); //显示推荐列表 showmore.setOnClickListener(new TextView.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub //显示推荐列表(综合) AppConnect.getInstance(MainActivity.this).showOffers(MainActivity.this); } }); /*myListViewOnItemClickListener */ myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { /*将myCursor移动到单机的值*/ myCursor.moveToPosition(arg2); /* 获取字段_id的值 */ _id = myCursor.getInt(0); /*取得字段todo_text的值*/ myEditText.setText(myCursor.getString(1)); } }); myListView .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { /* getSelectedItem所取得的是SQLiteCursor */ SQLiteCursor sc = (SQLiteCursor) arg0.getSelectedItem(); _id = sc.getInt(0); myEditText.setText(sc.getString(1)); } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. //getMenuInflater().inflate(R.menu.main, menu); super.onCreateOptionsMenu(menu); /* 添加三个 MENU */ menu.add(Menu.NONE, MENU_ADD, 0, R.string.strAddButton); menu.add(Menu.NONE, MENU_EDIT, 0, R.string.strEditButton); menu.add(Menu.NONE, MENU_DELETE, 0, R.string.strDeleteButton); return true; } public void addTodo(){ if (myEditText.getText().toString().equals("")) return; /*将数据添加到数据库*/ myToDoDB.insert(myEditText.getText().toString()); /* 重新查询 */ myCursor.requery(); /*刷新myListView */ myListView.invalidateViews(); myEditText.setText(""); _id = 0; } public void deleteTodo(){ if(_id == 0)return; myToDoDB.delete(_id); myCursor.requery(); myListView.invalidateViews(); myEditText.setText(""); _id=0; } public void editTodo(){ if (myEditText.getText().toString().equals("")) return; /*将数据添加到数据库*/ myToDoDB.update(_id,myEditText.getText().toString()); /* 重新查询 */ myCursor.requery(); /*刷新myListView */ myListView.invalidateViews(); myEditText.setText(""); _id = 0; } @Override protected void onDestroy() { // TODO Auto-generated method stub sound1.start(); super.onDestroy(); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/bg"> <LinearLayout android:id="@+id/miniAdLinearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> <EditText android:id="@+id/myEditText" android:layout_width="match_parent" android:layout_height="56dp" android:text="" android:textAppearance="?android:attr/textAppearanceLarge" /> <LinearLayout android:id="@+id/miniAdLinearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> <ListView android:id="@+id/myListView" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> <LinearLayout android:id="@+id/AdLinearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> <TextView android:id="@+id/showmore" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="更多应用下载~~" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <CheckedTextView android:id="@+id/myTextView1" android:layout_width="match_parent" android:layout_height="match_parent" android:text=" " /> </LinearLayout>
相关推荐
csdngamedev 2020-04-12
geeklibo 2020-04-07
SyntaxE 2020-03-15
专注移动互联 2020-02-23
ntfsformac 2020-02-11
vickay 2020-02-01
colin00 2020-02-14
fish 2019-06-25
RayDon 2013-08-11
Jesus0 2010-06-15
bluecarrot 2019-11-11
ourtimes 2019-11-11
cooldatabase 2019-11-08
87447007 2015-05-17
MoreWindowsBlog 2016-09-18
pcwindows 2016-11-06
archimedes 2019-09-27
SSS 2019-04-25
SoccerZZM 2018-08-20