TShopping

 找回密碼
 註冊
搜索
查看: 488|回復: 0

[教學] [Android智慧型手機設計_Ch8_2_SQLite瀏覽功能]

[複製鏈接]
發表於 2013-6-24 20:53:22 | 顯示全部樓層 |閱讀模式
 
Push to Facebook Push to Plurk Push to Twitter 
1.jpg

存取SQLite資料庫的說明如下:


SQLiteDatabase類別提供2種查詢功能的方法

1.rawQuery():僅接受單純的SQL查詢語法

rawQuery(String sql, String[] selectionArgs)

2.query():將SQL查詢語法依照query()的參數來切割
query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)


1.設計Ch8_2_BrowseData.java
  1. package com.example.ch8_2_browsedata;

  2. import java.util.ArrayList;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;

  11. public class Ch8_2_BrowseData extends Activity {
  12. EditText et1,et2,et3,et4;
  13. Button btn1,btn2;
  14. TextView tv2;
  15. DBHP dbHlp;
  16. ArrayList<Site> sites;
  17. int index=0;
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. connectDB();
  22. findViews();
  23. showSites(index);
  24. }
  25. private void connectDB() {
  26. if(dbHlp == null)
  27. dbHlp = new DBHP(this);
  28. dbHlp.fillDB();
  29. sites=dbHlp.getAllSites();
  30. }
  31. void findViews(){
  32. et1=(EditText)findViewById(R.id.editText1);
  33. et2=(EditText)findViewById(R.id.editText2);
  34. et3=(EditText)findViewById(R.id.editText3);
  35. et4=(EditText)findViewById(R.id.editText4);
  36. btn1=(Button)findViewById(R.id.button1);
  37. btn2=(Button)findViewById(R.id.button2);
  38. tv2=(TextView)findViewById(R.id.textView2);
  39. btn1.setOnClickListener(new OnClickListener(){

  40. @Override
  41. public void onClick(View v) {
  42. index=index+1;
  43. if(index>=sites.size())
  44. index=0;
  45. showSites(index);
  46. }} );
  47. btn2.setOnClickListener(new OnClickListener(){

  48. @Override
  49. public void onClick(View v) {
  50. index=index-1;
  51. if(index<0)
  52. index=sites.size()-1;
  53. showSites(index);
  54. }} );
  55. }
  56. void showSites(int index){
  57. if(sites.size()>0){
  58. tv2.setText((index+1)+"/"+sites.size()+getString(R.string.count));
  59. et1.setText(sites.get(index).getId());
  60. et2.setText(sites.get(index).getName());
  61. et3.setText(sites.get(index).getPhoneNo());
  62. et4.setText(sites.get(index).getAddress());
  63. }else{
  64. tv2.setText("0/0"+getString(R.string.count));
  65. et1.setText("");
  66. et2.setText("");
  67. et3.setText("");
  68. et4.setText("");
  69. }
  70. }
  71. }
複製代碼



2.設計DBHP.java
  1. package com.example.ch8_2_browsedata;

  2. import java.util.ArrayList;
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  8. import android.database.sqlite.SQLiteOpenHelper;

  9. public class DBHP extends SQLiteOpenHelper {
  10. private static final String DATABASE_NAME = "sites";
  11. private static final int DATABASE_VERSION = 1;
  12. private static final String TABLE_NAME = "sitesInfo";
  13. private static final String TABLE_CREATE =
  14. "CREATE TABLE " + TABLE_NAME + " (id TEXT NOT NULL,name TEXT NOT NULL,phoneNo TEXT, address TEXT, PRIMARY KEY (id)); ";
  15. public DBHP(Context context) {
  16. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  17. }
  18. @Override
  19. public void onCreate(SQLiteDatabase db) {
  20. db.execSQL(TABLE_CREATE);
  21. }
  22. @Override
  23. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  24. db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  25. onCreate(db);
  26. }
  27. public void fillDB() {
  28. SQLiteDatabase db = getWritableDatabase();
  29. ContentValues[] values = new ContentValues[3];
  30. for(int i=0; i<values.length; i++)
  31. values[i] = new ContentValues();

  32. values[0].put("id", "yangmingshan");
  33. values[0].put("name", "陽明山國家公園管理處");
  34. values[0].put("phoneNo", "02-28613601");
  35. values[0].put("address", "台北市北投區竹子湖路1之20號");

  36. values[1].put("id", "yushan");
  37. values[1].put("name", "玉山國家公園管理處");
  38. values[1].put("phoneNo", "049-2773121");
  39. values[1].put("address", "南投縣水里鄉中山路一段300號");

  40. values[2].put("id", "taroko");
  41. values[2].put("name", "太魯閣國家公園管理處");
  42. values[2].put("phoneNo", "03-8621100");
  43. values[2].put("address", "花蓮縣秀林鄉258號");

  44. for(ContentValues row : values){
  45. db.insert(TABLE_NAME, null, row);
  46. }
  47. db.close();
  48. }
  49. public ArrayList<Site> getAllSites()
  50. {
  51. SQLiteDatabase db = getReadableDatabase();
  52. String columns[]={"id","name","phoneNo","address"};
  53. Cursor cursor =db.query(TABLE_NAME, columns, null, null, null, null, null);
  54. ArrayList<Site> sites = new ArrayList<Site>();
  55. while(cursor.moveToNext()){
  56. String id=cursor.getString(0);
  57. String name=cursor.getString(1);
  58. String phoneNo=cursor.getString(2);
  59. String address=cursor.getString(3);
  60. Site site=new Site(id,name,phoneNo,address);
  61. sites.add(site);
  62. }
  63. cursor.close();
  64. db.close();
  65. return sites;
  66. }
  67. }
複製代碼



3.設計Site.java
  1. package com.example.ch8_2_browsedata;

  2. public class Site {
  3. String id,name,phoneNo,address;
  4. Site(String id,String name,String phoneNo,String address)
  5. {
  6. this.id=id;
  7. this.name=name;
  8. this.phoneNo=phoneNo;
  9. this.address=address;
  10. }
  11. String getId(){
  12. return id;
  13. }
  14. String getName(){
  15. return name;
  16. }
  17. String getPhoneNo(){
  18. return phoneNo;
  19. }
  20. String getAddress(){
  21. return address;
  22. }
  23. }
複製代碼



 

臉書網友討論
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2016-12-7 11:54 , Processed in 0.058185 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表