安卓初級教程(1):@Database(1)
package com.example.android.db01; import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener; public class SQLiteTest extends Activity {
OnClickListener listener_add = null;
OnClickListener listener_update = null;
OnClickListener listener_delete = null;
OnClickListener listener_clear = null;
Button button_add;
Button button_update;
Button button_delete;
Button button_clear;
DBConnection helper;
public int id_this;
public interface UserSchema {
String TABLE_NAME = "Users"; //Table Name
String ID = "_id"; //ID
String USER_NAME = "user_name"; //User Name
String ADDRESS = "address"; //Address
String TELEPHONE = "telephone"; //Phone Number
String MAIL_ADDRESS = "mail_address"; //Mail Address
}
/** Called when the activity is first created. */
//SQLiteTest
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText mEditText01 = (EditText)findViewById(R.id.EditText01);
final EditText mEditText02 = (EditText)findViewById(R.id.EditText02);
final EditText mEditText03 = (EditText)findViewById(R.id.EditText03);
final EditText mEditText04 = (EditText)findViewById(R.id.EditText04); helper = new DBConnection(this);
final SQLiteDatabase db = helper.getWritableDatabase();
final String[] FROM =
{
UserSchema.ID,
UserSchema.USER_NAME,
UserSchema.TELEPHONE,
UserSchema.ADDRESS,
UserSchema.MAIL_ADDRESS
};
//基本演算法,計算出db數數庫中總共有多少項數據列。
Cursor c = db.query(UserSchema.TABLE_NAME, new String[] {UserSchema.USER_NAME}, null, null, null, null, null);
c.moveToFirst();
CharSequence[] list = new CharSequence[c.getCount()];
for (int i = 0; i < list.length; i++) {
list[i] = c.getString(0);
c.moveToNext();
}
c.close();
//
Spinner spinner = (Spinner)findViewById(R.id.Spinner01);
spinner.setAdapter(new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, list)); spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String user_name = ((Spinner)parent).getSelectedItem().toString();
Cursor c = db.query("Users", FROM , "user_name='" + user_name + "'", null, null, null, null);
c.moveToFirst();
id_this = Integer.parseInt(c.getString(0));
String user_name_this = c.getString(1);
String telephone_this = c.getString(2);
String address_this = c.getString(3);
String mail_address_this = c.getString(4);
c.close();
mEditText01.setText(user_name_this);
mEditText02.setText(telephone_this);
mEditText03.setText(address_this);
mEditText04.setText(mail_address_this);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
//增加資料,定義項是ContentValues,獲取資料時使用put,然後用SQLiteDatanase的方法去開啟數據庫,最後將資料輸入database,用的方法是db.insert
listener_add = new OnClickListener() {
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(UserSchema.USER_NAME, mEditText01.getText().toString());
values.put(UserSchema.TELEPHONE, mEditText02.getText().toString());
values.put(UserSchema.ADDRESS, mEditText03.getText().toString());
values.put(UserSchema.MAIL_ADDRESS, mEditText04.getText().toString());
SQLiteDatabase db = helper.getWritableDatabase();
db.insert(UserSchema.TABLE_NAME, null, values);
db.close();
onCreate(savedInstanceState);
}
};
//更新資料,定義項是ContentValues,獲取資料時使用put,然後用SQLiteDatanase的方法去開啟數據庫,最後將資料輸入database,用的方法是db.update
listener_update = new OnClickListener() {
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(UserSchema.USER_NAME, mEditText01.getText().toString());
values.put(UserSchema.TELEPHONE, mEditText02.getText().toString());
values.put(UserSchema.ADDRESS, mEditText03.getText().toString());
values.put(UserSchema.MAIL_ADDRESS, mEditText04.getText().toString());
String where = UserSchema.ID + " = " + id_this;
SQLiteDatabase db = helper.getWritableDatabase();
db.update(UserSchema.TABLE_NAME, values, where ,null);
db.close();
onCreate(savedInstanceState);
}
};
//[Delete]
listener_delete = new OnClickListener() {
public void onClick(View v) {
String where = UserSchema.ID + " = " + id_this;
SQLiteDatabase db = helper.getWritableDatabase();
db.delete(UserSchema.TABLE_NAME, where ,null);
db.close();
onCreate(savedInstanceState);
}
};
//[Clear]
listener_clear = new OnClickListener() {
public void onClick(View v) {
mEditText01.setText("");
mEditText02.setText("");
mEditText03.setText("");
mEditText04.setText("");
}
};
//BUTTON0i,i=1,2,3,4 OnClickListener
button_add = (Button)findViewById(R.id.Button01);
button_add.setOnClickListener(listener_add);
button_update = (Button)findViewById(R.id.Button02);
button_update.setOnClickListener(listener_update);
button_delete = (Button)findViewById(R.id.Button03);
button_delete.setOnClickListener(listener_delete);
button_clear = (Button)findViewById(R.id.Button04);
button_clear.setOnClickListener(listener_clear);
}
//SQLiteOpenHelper,創建資料庫
public static class DBConnection extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "PhoneBookDB";
private static final int DATABASE_VERSION = 1;
private DBConnection(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + UserSchema.TABLE_NAME + " ("
+ UserSchema.ID + " INTEGER primary key autoincrement, "
+ UserSchema.USER_NAME + " text not null, "
+ UserSchema.TELEPHONE + " text not null, "
+ UserSchema.ADDRESS + " text not null, "
+ UserSchema.MAIL_ADDRESS + " text not null "+ ");";
//Log.i("haiyang:createDB=", sql);
db.execSQL(sql);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
安卓初級教程(1):@Database(1)的更多相关文章
- 安卓初級教程(4):sqlite建立資料庫
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...
- 安卓初級教程(3):ContentProvider的運用原理
package com.example.android.provider; import java.util.ArrayList; import java.util.HashMap; import j ...
- 安卓初級教程(5):TabHost的思考
package com.myhost; import android.os.Bundle; import android.view.LayoutInflater; import android.wid ...
- 安卓初級教程(2):SD創建file,儲存與讀寫的方法(1)
package com.sdmadik; import java.io.*; import android.app.Activity; import android.os.Bundle; import ...
- 安卓中級教程(3):ScrollView
以上是scrollview的圖例,可見srollview是一種滑動功能的控件,亦是非常常見的控件. 一般寫法如下: package com.mycompany.viewscroller; import ...
- 安卓中級教程(10):@InjectView
package com.example.android.db01; import android.app.Activity; import android.content.ContentValues; ...
- 安卓中級教程(6):annotation的基本用法
package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...
- 安卓中級教程(1):@InjectView
package com.mycompany.hungry; import android.annotation.SuppressLint; import android.app.Activity; i ...
- 安卓中級教程(11):深入研究餓了麼的各個java檔運作關係(1)
package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...
随机推荐
- h5页面 禁止缩放
<head><meta name="viewport" content="width=device-width,minimum-scale=1.0,ma ...
- Python入门(二)
Python版本:Python 2.7.5 1.列表切片 >>> numbers = [1,2,3,5,6,7,8] >>> numbers[3] 5 >&g ...
- alert 替代效果smoke.js
在一些表单等需要弹窗提醒的时候,每个浏览器都有一个alert(),comfirm()函数能弹出信息,但是浏览器的自带的这种效果样式不统一,而且都固定在页面顶部: smoke.js轻量级的JS插件,他标 ...
- windows7下php5.4成功安装imageMagick,及解决php imagick常见错误问题。(phpinfo中显示不出来是因为:1.imagick软件本身、php本身、php扩展三方版本要一致,2.需要把CORE_RL_*.dll多个文件放到/php/目录下面)
windows7下 php5.4成功安装imageMagick . (phpinfo中显示不出来是因为:1.软件本身.php本身.php扩展三方版本要一致,2.需要把CORE_RL_*.dll多个 ...
- Nginx概念及基础安装--详细讲解
1.主要内容: Nginx的基础 特性 配置部署 优化(了解) 2.Nginx 是什么? Nginx是一个开源的,支持高性能,高并发的www ...
- js框架设计1.2对象扩展笔记
需要一个新的功能添加到我们的命名空间上.这方法在JS中被叫做extend或者mixin,若是遍历属性用一下1.1代码,则会遍历不出原型方法,所以1.2介绍的是mass Framework里的mix方法 ...
- nginx+ISS 负载均衡 快速入门
第一:下载 http://pan.baidu.com/s/1dDwapbF 或者官网 http://nginx.org/en/download.html 启动服务: 直接运行nginx.exe,缺点控 ...
- appium for mobile web 之使用 ChromeDriver
之前研究了一段时间的appium for native app 相应的总结如下: appium测试环境搭建 :ht ...
- 毛笔笔锋算法IOS版
http://www.merowing.info/2012/04/drawing-smooth-lines-with-cocos2d-ios-inspired-by-paper/#.VUln2_mqp ...
- JAVA字符串格式化-String.format()的使用(转)
常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...