在main.xml布局文件中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#000000">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<Button
android:id="@+id/insertBut"
android:background="#0066ff"
android:layout_width="50dp"
android:layout_height="40dp"
android:textColor="#ffffff"
android:text="增加" />
<Button
android:id="@+id/updateBut"
android:background="#0066ff"
android:layout_marginLeft="8dp"
android:textColor="#ffffff"
android:layout_width="50dp"
android:layout_height="40dp"
android:text="更新" />
<Button
android:id="@+id/deleteBut"
android:layout_marginLeft="8dp"
android:textColor="#ffffff"
android:background="#0066ff"
android:layout_width="50dp"
android:layout_height="40dp"
android:text="删除" />
<Button
android:id="@+id/queryBut"
android:layout_marginLeft="8dp"
android:background="#0066ff"
android:textColor="#ffffff"
android:layout_width="50dp"
android:layout_height="40dp"
android:text="查询" />
</LinearLayout>
<TextView
android:id="@+id/mainInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/memberList"
android:background="#0066ff"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
在member.xml布局文件中:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0066ff">
<TableRow>
<TextView
android:id="@+id/_id"
android:layout_height="30dp"
android:layout_marginLeft="8dp"
android:textColor="#ffffff"
android:layout_width="30px" />
<TextView
android:id="@+id/name"
android:textColor="#ffffff"
android:layout_height="30dp"
android:layout_width="100px" />
<TextView
android:id="@+id/age"
android:layout_height="30dp"
android:textColor="#ffffff"
android:layout_width="30px" />
<TextView
android:id="@+id/birthday"
android:textColor="#ffffff"
android:layout_height="30dp"
android:layout_width="150px" />
</TableRow>
</TableLayout>
在MyContentProviderDemo.java中:
package com.li.cp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MyContentProviderDemo extends Activity {
private Button insertBut = null ; // 操作按钮
private Button updateBut = null ;
private Button deleteBut = null ;
private Button queryBut = null ;
private TextView mainInfo = null ;
private ListView memberList = null ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mainInfo = (TextView) super.findViewById(R.id.mainInfo) ; // 取得组件
this.insertBut = (Button) super.findViewById(R.id.insertBut) ; // 取得组件
this.updateBut = (Button) super.findViewById(R.id.updateBut) ; // 取得组件
this.deleteBut = (Button) super.findViewById(R.id.deleteBut) ; // 取得组件
this.queryBut = (Button) super.findViewById(R.id.queryBut) ; // 取得组件
this.memberList = (ListView) super.findViewById(R.id.memberList) ; // 取得组件
this.insertBut.setOnClickListener(new InsertOnClickListener()) ;
this.updateBut.setOnClickListener(new UpdateOnClickListener()) ;
this.deleteBut.setOnClickListener(new DeleteOnClickListener()) ;
this.queryBut.setOnClickListener(new QueryOnClickListener()) ;
}
private class InsertOnClickListener implements OnClickListener {
public void onClick(View v) {
MyContentProviderDemo.this.mainInfo.setText("执行的是增加操作...") ;
long id = 0 ; // 接收返回的id数据
id = MyContentProviderDemo.this.testInsert("李叶文", 24,
new SimpleDateFormat("yyyy-MM-dd").format(new Date())); // 增加数据
Toast.makeText(MyContentProviderDemo.this, "数据增加成功,ID为:" + id,
Toast.LENGTH_LONG).show();
}
}
private class DeleteOnClickListener implements OnClickListener {
public void onClick(View v) {
MyContentProviderDemo.this.mainInfo.setText("执行的是删除操作...") ;
long result = 0 ; // 接收返回的id数据
result = MyContentProviderDemo.this.testDelete(String.valueOf(4)); // 更新数据
Toast.makeText(MyContentProviderDemo.this, "删除了" + result + "条记录",
Toast.LENGTH_LONG).show();
}
}
private class QueryOnClickListener implements OnClickListener {
public void onClick(View v) {
MyContentProviderDemo.this.mainInfo.setText("执行的是查询操作...") ;
Cursor result = MyContentProviderDemo.this.testQuery(null) ; // 查询全部
MyContentProviderDemo.this.startManagingCursor(result) ; // 结果集交给系统管理
List<Map<String,Object>> members = new ArrayList<Map<String,Object>>() ;
for (result.moveToFirst(); !result.isAfterLast(); result
.moveToNext()) {
Map<String,Object> member = new HashMap<String,Object>() ;
member.put("_id", result.getInt(0)) ;
member.put("name", result.getString(1)) ;
member.put("age", result.getInt(2)) ;
member.put("birthday", result.getString(3)) ;
members.add(member) ;
}
MyContentProviderDemo.this.memberList
.setAdapter(new SimpleAdapter(MyContentProviderDemo.this,
members, R.layout.member, new String[] { "_id",
"name", "age", "birthday" }, new int[] {
R.id._id, R.id.name, R.id.age,
R.id.birthday }));
Toast.makeText(MyContentProviderDemo.this, "数据查询成功!",
Toast.LENGTH_LONG).show();
}
}
private class UpdateOnClickListener implements OnClickListener {
public void onClick(View v) {
MyContentProviderDemo.this.mainInfo.setText("执行的是更新操作...") ;
long result = 0 ; // 接收返回的id数据
result = MyContentProviderDemo.this.testUpdate("", "LYW", 18,
"1989-09-19"); // 更新数据
Toast.makeText(MyContentProviderDemo.this, "更新了" + result + "条记录",
Toast.LENGTH_LONG).show();
}
}
private long testInsert(String name,int age,String birthday) { // 执行增加操作
ContentResolver contentResolver = super.getContentResolver() ; // 取得ContentResolver对象
ContentValues values = new ContentValues() ;
values.put(MLDNDatabaseMetaData.MemberTableMetaData.MEMBER_NAME, name) ;
values.put(MLDNDatabaseMetaData.MemberTableMetaData.MEMBER_AGE, age) ;
values.put(MLDNDatabaseMetaData.MemberTableMetaData.MEMBER_BIRTHDAY, birthday) ;
Uri resultUri = contentResolver.insert(
MLDNDatabaseMetaData.MemberTableMetaData.CONTENT_URI, values);
return ContentUris.parseId(resultUri) ; // 解析出返回的id数据
}
private long testUpdate(String _id, String name, int age, String birthday) { // 执行增加操作
long result = 0 ; // 返回结果
ContentResolver contentResolver = super.getContentResolver() ; // 取得ContentResolver对象
ContentValues values = new ContentValues() ;
values.put(MLDNDatabaseMetaData.MemberTableMetaData.MEMBER_NAME, name) ;
values.put(MLDNDatabaseMetaData.MemberTableMetaData.MEMBER_AGE, age) ;
values.put(MLDNDatabaseMetaData.MemberTableMetaData.MEMBER_BIRTHDAY, birthday) ;
if(_id == null || "".equals(_id)) { // 更新全部
result = contentResolver.update(MLDNDatabaseMetaData.MemberTableMetaData.CONTENT_URI, values, null, null) ;
} else { // 按照id更新
result = contentResolver.update(
Uri.withAppendedPath(MLDNDatabaseMetaData.MemberTableMetaData.CONTENT_URI, _id),
values, null, null);
}
return result ; // 解析出返回的id数据
}
private long testDelete(String _id) {
ContentResolver contentResolver = super.getContentResolver() ; // 取得ContentResolver对象
long result = 0 ;
if (_id == null || "".equals(_id)) {
result = contentResolver.delete(
MLDNDatabaseMetaData.MemberTableMetaData.CONTENT_URI, null,
null);
} else {
result = contentResolver.delete(Uri.withAppendedPath(
MLDNDatabaseMetaData.MemberTableMetaData.CONTENT_URI, _id),
null, null);
}
return result ;
}
private Cursor testQuery(String id) {
if(id == null || "".equals(id)) { // 查询全部
return super.getContentResolver().query(
MLDNDatabaseMetaData.MemberTableMetaData.CONTENT_URI, null,
null, null,
MLDNDatabaseMetaData.MemberTableMetaData.SORT_ORDER);
} else {
return super
.getContentResolver()
.query(Uri.withAppendedPath(
MLDNDatabaseMetaData.MemberTableMetaData.CONTENT_URI,
id), null, null, null,
MLDNDatabaseMetaData.MemberTableMetaData.SORT_ORDER);
}
}
}
在MemberContentProvider.java中:
package com.li.cp;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class MemberContentProvider extends ContentProvider {
private static UriMatcher uriMatcher = null ; // 地址转换
private static final int GET_MEMBER_LIST = 1 ; // 得到全部的数据
private static final int GET_MEMBER_ITEM = 2 ; // 取得一个数据
private MyDatabaseHelper helper = null ;
static { // 静态代码块
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH) ; // 实例化对象
uriMatcher.addURI(MLDNDatabaseMetaData.AUTHORITY,
MLDNDatabaseMetaData.MemberTableMetaData.TABLE_NAME,
GET_MEMBER_LIST); // 取得全部数据的匹配地址
uriMatcher.addURI(MLDNDatabaseMetaData.AUTHORITY,
MLDNDatabaseMetaData.MemberTableMetaData.TABLE_NAME + "/#",
GET_MEMBER_ITEM); // 取得一个数据的匹配地址
}
@Override
public String getType(Uri uri) { // 取得数据的类型的
switch (uriMatcher.match(uri)) { // 匹配传入进来的Uri的类型
case GET_MEMBER_LIST:
return MLDNDatabaseMetaData.MemberTableMetaData.CONTACT_LIST;
case GET_MEMBER_ITEM:
return MLDNDatabaseMetaData.MemberTableMetaData.CONTACT_ITEM;
default:
throw new UnsupportedOperationException("Not Support Operation :"
+ uri);
}
}
@Override
public boolean onCreate() {
this.helper = new MyDatabaseHelper(super.getContext()) ;
return true; // 操作成功了
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = this.helper.getWritableDatabase() ;// 以写方式打开数据库
int result = 0 ; // 操作结果
switch (uriMatcher.match(uri)) {
case GET_MEMBER_LIST:
result = db.delete(
MLDNDatabaseMetaData.MemberTableMetaData.TABLE_NAME,
selection, selectionArgs); // 删除全部
break ;
case GET_MEMBER_ITEM:
long id = ContentUris.parseId(uri) ; // 找到一个数据的ID
String where = "_id=" + id ;
result = db.delete(
MLDNDatabaseMetaData.MemberTableMetaData.TABLE_NAME, where,
selectionArgs);
break ;
default:
throw new UnsupportedOperationException("Not Support Operation :"
+ uri);
}
return result;
}
@Override
public Uri insert(Uri uri, ContentValues values) { // content://org.lxh.demo.membercontentprovider/member
SQLiteDatabase db = this.helper.getWritableDatabase() ;// 以写方式打开数据库
long id = 0 ; // 取得增长后的数据ID
switch(uriMatcher.match(uri)) {
case GET_MEMBER_LIST :
id = db.insert(MLDNDatabaseMetaData.MemberTableMetaData.TABLE_NAME,
MLDNDatabaseMetaData.MemberTableMetaData._ID, values);
String uriPath = uri.toString() ;
String path = uriPath + "/" + id ;
return Uri.parse(path) ;
case GET_MEMBER_ITEM :
return null ;
default:
throw new UnsupportedOperationException("Not Support Operation :"
+ uri);
}
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = this.helper.getWritableDatabase() ;// 以写方式打开数据库
switch (uriMatcher.match(uri)) {
case GET_MEMBER_LIST:
return db
.query(MLDNDatabaseMetaData.MemberTableMetaData.TABLE_NAME,
projection, selection, selectionArgs, null, null,
sortOrder);
case GET_MEMBER_ITEM:
long id = ContentUris.parseId(uri) ; // 找到ID
String where = "_id=" + id ;
return db
.query(MLDNDatabaseMetaData.MemberTableMetaData.TABLE_NAME,
projection, where, selectionArgs, null, null,
sortOrder);
default:
throw new UnsupportedOperationException("Not Support Operation :"
+ uri);
}
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = this.helper.getWritableDatabase() ;// 以写方式打开数据库
int result = 0 ; // 表示结果
switch (uriMatcher.match(uri)) {
case GET_MEMBER_LIST:
result = db.update(
MLDNDatabaseMetaData.MemberTableMetaData.TABLE_NAME,
values, null, null);
break ;
case GET_MEMBER_ITEM:
long id = ContentUris.parseId(uri) ; // 找到里面的id
String where = "_id=" + id ;
result = db.update(
MLDNDatabaseMetaData.MemberTableMetaData.TABLE_NAME,
values, where, selectionArgs);
break ;
default:
throw new UnsupportedOperationException("Not Support Operation :"
+ uri);
}
return result ;
}
}
在MLDNDatabaseMetaData.java中:
package com.li.cp;
import android.net.Uri;
import android.provider.BaseColumns;
public interface MLDNDatabaseMetaData {
// 这个是外部访问的Authority,Content地址为:content//com.li.cp.membercontentprovider
public static final String AUTHORITY = "com.li.cp.membercontentprovider" ;
// 数据库的名字
public static final String DATABASE_NAME = "liyewen" ; // 操作的数据库的名字
public static final int VERSION = 1 ; // 数据库的版本
public static interface MemberTableMetaData extends BaseColumns{
public static final String TABLE_NAME = "member" ; // 表名称
// 外部程序访问本表的Uri地址,而且名称都统一设置为CONTENT_URI
public static final Uri CONTENT_URI = Uri.parse("content://"
+ AUTHORITY + "/" + TABLE_NAME);
// 得到member表中的全部记录
public static final String CONTACT_LIST = "vnd.android.cursor.dir/vnd.mldncontentprovider.member" ;
// 取得一个member的信息,相当于就是按照ID查询
public static final String CONTACT_ITEM = "vnd.android.cursor.item/vnd.mldncontentprovider.member" ;
public static final String MEMBER_NAME = "name" ;
public static final String MEMBER_AGE = "age" ;
public static final String MEMBER_BIRTHDAY = "birthday" ;
public static final String SORT_ORDER = "_id DESC" ;// 排序操作
}
}
在MyDatabaseHelper.java中:
package com.li.cp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDatabaseHelper extends SQLiteOpenHelper {
public MyDatabaseHelper(Context context) {
super(context, MLDNDatabaseMetaData.DATABASE_NAME, null,
MLDNDatabaseMetaData.VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE "
+ MLDNDatabaseMetaData.MemberTableMetaData.TABLE_NAME + " ( "
+ MLDNDatabaseMetaData.MemberTableMetaData._ID
+ " INTEGER PRIMARY KEY ,"
+ MLDNDatabaseMetaData.MemberTableMetaData.MEMBER_NAME
+ " VARCHAR(50) NOT NULL ,"
+ MLDNDatabaseMetaData.MemberTableMetaData.MEMBER_AGE
+ " INTEGER NOT NULL ,"
+ MLDNDatabaseMetaData.MemberTableMetaData.MEMBER_BIRTHDAY
+ " DATE NOT NULL" + " )";
db.execSQL(sql) ;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS " + MLDNDatabaseMetaData.MemberTableMetaData.TABLE_NAME ;
db.execSQL(sql) ;
this.onCreate(db) ;
}
}
github上的jfeinstein10的SlidingMenu就不多说了,地址:https://github.com/jfeinstein10/SlidingMenu,下面记录下如何在eclipse上的部署:
1、下载SlidingMenu,地址如上;
2、下载ActiongBarsSherlock,地址:http://actionbarsherlock.com/ ;
3、解压到各自的文件夹;
4、首先导入ActiongBarsSherlock(重命名Actionbarsherlock_library):
5、然后导入SlidingMenu的library(重命名:SlidingMenu_library_jfeinstein10):
6、删除第五步中导入的library(SlidingMenu_library_jfeinstein10)中的libs目录中的android-support-v4.jar,这个是为了避免冲突,右击SlidingMenu_library_jfeinstein10,选择属性,做如下设置:
7、导入SlidingMenu中的Example,删除它的android-support-v4.jar,并引用SlidingMenu_library_jfeinstein10,如下:
8、此时会报错,找到SlidingFragmentActivity,做如下修改即可:
参考:http://stackoverflow.com/questions/14057367/exact-procedure-to-add-jfeinstein10-slidingmenu-in-android-project/14118683#14118683
ContentObserver:内容观察者。它的一个重要的作用就在于实现内容的实时更新。。
它的基本使用如下:
1、注册观察者
在合适的地方(一般是在Activity中)写上以下代码:
Uri uri = Uri.parse("content://jd_love_ztmm"); getContentResolver().registerContentObserver(uri,true,new MyContentObserver());
(一般是在内容提供者ContentProvider中)
getContext().getContentResolver().notifyChange(uri,null);
通过以上的步骤,就能实现数据的实时更新了