Android开发:自动补全与SQLite联合的例子


从上一个例子(Android自动补全教程  )可以看到自动补全是很简单的,今天再深入一点,ArrayAdapter提供的字符串从数据库中查询,并且使用MultiAutoCompleteTextView控件。

此控件和AutoCompleteTextView的最大区别是可以补全多个词,看名字就能知道,呵呵。

效果如下,每个词中间用逗号分割。


首先

布局和上一个例子相同。

创建一个名为list_item.xml的XML文件并把它保存在res/layout/文件夹下。编辑文件像下面这样:

[html]
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:layout_width="fill_parent"    
  4.     android:layout_height="fill_parent"    
  5.     android:padding="10dp"    
  6.     android:textSize="16sp"    
  7.     android:textColor="#000">    
  8. </TextView>   

这个文件定义了一个简单的TextView来显示提示列表的每一项。

打开 res/layout/main.xml文件加入如下内容:

[html]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/tv"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/hello" />  
  12.     <MultiAutoCompleteTextView   
  13.         android:id="@+id/mactv"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         />  
  17. </LinearLayout>  

下面就来设计我的数据库,名字为person,要建一个person表,有两个字段:name和gender。

新建一个SQLiteHelper类,继承自SQLiteOpenHelper:

[java]
  1. package com.linc.autosqlite.dao;  
  2.   
  3.   
  4. import android.content.Context;  
  5. import android.database.Cursor;  
  6. import android.database.sqlite.SQLiteDatabase;  
  7. import android.database.sqlite.SQLiteOpenHelper;  
  8.   
  9. /** 
  10.  * 实现对表的创建、更新、变更列名操作 
  11.  * @author lincyang 
  12.  * 
  13.  */  
  14. public class SQLiteHelper extends SQLiteOpenHelper {  
  15.     public static final String DB_NAME = "person";  
  16.     public static final int DB_VERSION = 1;  
  17.     protected static Context ctx;  
  18.   
  19.     //   
  20.     //构造函数一:传context   
  21.     //   
  22.     public SQLiteHelper(Context context) {  
  23.         super(context, DB_NAME, null, DB_VERSION);  
  24.         ctx = context;  
  25.     }  
  26.     //   
  27.     //构造函数二   
  28.     //   
  29.     public SQLiteHelper() {  
  30.         super(ctx,DB_NAME, null, DB_VERSION);  
  31.     }  
  32.       
  33.     @Override  
  34.     public void onCreate(SQLiteDatabase db) {  
  35.         String sql = "create table person(name varchar(20) not null , " +  
  36.                 "gender varchar(10) not null );";  
  37.         db.execSQL(sql);  
  38.     }  
  39.       
  40.     @Override  
  41.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  42.         // TODO Auto-generated method stub   
  43.           
  44.     }  
  45.     protected void closeCursor(Cursor cursor) {  
  46.         if (cursor != null) {  
  47.             cursor.close();  
  48.         }  
  49.     }  
  50. }  
  • 1
  • 2
  • 3
  • 下一页

相关内容