1.在SQLiteOpenHelper中,声明数据库名,并创建表
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "bbbcontact.db"; private final static int VERSION = 3; public DBHelper(Context context) { super(context, DATABASE_NAME, null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE contactstable ("+ "_id INTEGER PRIMARY KEY , "+ "name TEXT, " + "tel TEXT);"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS contactstable"); onCreate(db); } }
2.ContentProvider中 CONTENT_URI声明
private static UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
public static final String AUTHORITY = "com.bbb.contact";
private static final int CONTACTS = 1;
public static final Uri CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/contacts");
其中AUTHORITY要在mainfest.xml文件中声明:
<provider android:name="ContactProvider" android:authorities="com.bbb.contact"/>
在oncreate()函数中 new一个sqlitopenhelper的对象
public boolean onCreate() {
dbHelper = new DBHelper(getContext());
return true;
}
插入函数代码:
SQLiteDatabase db = dbHelper.getWritableDatabase();
long rowID = db.insert("contactstable", null, values);
if(rowID > 0){
Uri url = Uri.parse("content://" + AUTHORITY + "/contactstalbe" + "/" + rowID);
Log.i("",".............run insert.........url="+url);
return url;
}
ContactProvider.java代码:
import android.content.ContentProvider; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.util.Log; public class ContactProvider extends ContentProvider { private DBHelper dbHelper; private static UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH); public static final String AUTHORITY = "com.bbb.contact"; private static final int CONTACTS = 1; public static final Uri CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/contacts"); /* static{ URI_MATCHER.addURI(AUTHORITY, "contactstable", CONTACTS); } */ public ContactProvider(){ } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { return 0; } @Override public String getType(Uri uri) { return null; } @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = dbHelper.getWritableDatabase(); long rowID = db.insert("contactstable", null, values); if(rowID > 0){ Uri url = Uri.parse("content://" + AUTHORITY + "/contactstalbe" + "/" + rowID); Log.i("",".............run insert.........url="+url); return url; } return null; } @Override public boolean onCreate() { dbHelper = new DBHelper(getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = dbHelper.getWritableDatabase(); return db.query("contactstable", projection, selection, selectionArgs, null, null, sortOrder); } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; } }
3.数据添加到数据库
在添加数据界面,点击保存按钮,调用如下代码:
if(v == saveBtn){ ContentValues values = new ContentValues(); values.put("name", nameText.getText().toString()); values.put("tel", telText.getText().toString()); getContentResolver().insert(ContactProvider.CONTENT_URI, values); // provider.insert(CONTENT_URI, values); }
4.主函数listView数据显示
Uri uri = ContactProvider.CONTENT_URI;//getIntent().getData(); Log.i("",".................uri.........."+uri); Cursor c = getContentResolver().query(uri, null, null, null, null); startManagingCursor(c); //c.moveToFirst(); ListAdapter adapter = new SimpleCursorAdapter(this,R.layout.contact_list_item, c, new String[] {"name"}, new int[] {R.id.name_list}); setListAdapter(adapter);
其中getContentResolver().query(uri, null, null, null, null)与provider中的query关联。
R.layout.contact_list_item为所显示的listview的布局
new String[] {"name"}为创建的数据表的列名
new int[] {R.id.name_list}为显示项对应的id值,在R.layout.contact_list中