当前位置:文档之家› android 自定义ContentProvider的实现

android 自定义ContentProvider的实现

android 自定义ContentProvider的实现
android 自定义ContentProvider的实现

android 自定义ContentProvider的实现:

1.定义一个MainActivity.java

package com.amaker.ch10.app;

import com.amaker.ch10.app.Employees.Employee;

import android.app.Activity;

import android.content.ContentUris;

import android.content.ContentValues;

import android.database.Cursor;

import https://www.doczj.com/doc/cb1454757.html,.Uri;

import android.os.Bundle;

import android.util.Log;

public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(https://www.doczj.com/doc/cb1454757.html,yout.main);

Insert();

query();

update();

query();

del();

query();

}

private void del() {

Uri uri=ContentUris.withAppendedId(Employee.CONTENT_URI, 1);

getContentResolver().delete(uri, null, null);

}

private void update() {

Uri uri=ContentUris.withAppendedId(Employee.CONTENT_URI, 1);

ContentValues values=new ContentValues();

values.put(https://www.doczj.com/doc/cb1454757.html,, "hz.guo");

values.put(Employee.GENDER, "male");

values.put(Employee.AGE, 31);

getContentResolver().update(uri, values, null, null);

}

private void query() {

String []projection=new String[]{

Employee._ID,

https://www.doczj.com/doc/cb1454757.html,,

Employee.GENDER,

Employee.AGE

};

Cursor cursor=managedQuery(Employee.CONTENT_URI, projection, null, null, Employee.DEFAULT_SORT_ORDER);

if(cursor.moveToFirst()){

for(int i=0;i

cursor.moveToPosition(i);

String name=cursor.getString(1);

String gender=cursor.getString(2);

String age =cursor.getString(3);

Log.i("emp", name+":"+gender+":"+age);

}

}

}

private void Insert() {

ContentValues values =new ContentValues();

values.put(https://www.doczj.com/doc/cb1454757.html,, "amaker");

values.put(Employee.GENDER, "male");

values.put(Employee.AGE, 30);

getContentResolver().insert(Employee.CONTENT_URI,values);

}

}

2.修改AndroidManifest.xml

package="com.amaker.ch10.app"

android:versionCode="1"

android:versionName="1.0" >

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

android:name=".MainActivity"

android:label="@string/app_name" >

android:authorities="com.amaker.provider.Employees"/>

3.定义一个DBHelper.java

package com.amaker.ch10.app;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

private static final String DATEBASE_NAME="Employee.db";

private static final int DATEBASE_VERSION=1;

public static final String EMPLOYEES_TABLE_NAME="employee";

public DBHelper(Context context) {

super(context, DATEBASE_NAME, null, DATEBASE_VERSION);

}

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE "+EMPLOYEES_TABLE_NAME+"("

+Employees.Employee._ID+" INTEGER PRIMARY KEY,"

+https://www.doczj.com/doc/cb1454757.html,+" TEXT,"

+Employees.Employee.GENDER+" TEXT,"

+Employees.Employee.AGE+" INTEGER"

+");");

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS employee");

onCreate(db);

}

}

4.定义一个javabean类Employees.java

package com.amaker.ch10.app;

import https://www.doczj.com/doc/cb1454757.html,.Uri;

import android.provider.BaseColumns;

public final class Employees {

public static final String AUTHORITY="com.amaker.provider.Employees";

public Employees(){}

public static final class Employee implements BaseColumns{

private Employee(){}

public static final Uri CONTENT_URI=Uri.parse("content://"+AUTHORITY+"/employee");

public static final String CONTENT_TYPE="vnd.android.cursor.dir/vnd.amaker.employees";

public static final String CONTENT_ITEM_TYPE_STRING="vnd.android.cursor.item/vnd.amaker.employees";

public static final String DEFAULT_SORT_ORDER="name DESC";

public static final String NAME="name";

public static final String GENDER="gender";

public static final String AGE="age";

}

}

5.定义一个内容提供者类EmployeeProvider.java

package com.amaker.ch10.app;

import java.util.HashMap;

import com.amaker.ch10.app.Employees.Employee;

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.database.sqlite.SQLiteQueryBuilder;

import https://www.doczj.com/doc/cb1454757.html,.Uri;

import android.text.TextUtils;

import android.util.Log;

public class EmployeeProvider extends ContentProvider {

private DBHelper dbHelper;

private static UriMatcher sUriMatcher=null;

private static final int EMPLOYEE=1;

private static final int EMPLOYEE_ID=2;

private static HashMap empProjectionMap;

static{

sUriMatcher=new UriMatcher(UriMatcher.NO_MATCH);

sUriMatcher.addURI(Employees.AUTHORITY, "employee", EMPLOYEE);

sUriMatcher.addURI(Employees.AUTHORITY, "employee/#", EMPLOYEE_ID);

empProjectionMap=new HashMap();

empProjectionMap.put(Employees.Employee._ID,Employees.Employee._ID);

empProjectionMap.put(https://www.doczj.com/doc/cb1454757.html,,https://www.doczj.com/doc/cb1454757.html,);

empProjectionMap.put(Employees.Employee.GENDER,Employees.Employee.GENDER);

empProjectionMap.put(Employees.Employee.AGE,Employees.Employee.AGE);

}

public EmployeeProvider() {

}

@Override

public boolean onCreate() {

dbHelper=new DBHelper(getContext());

return true;

}

@Override

public Cursor query(Uri uri, String[] projection, String selection,

String[] selectionArgs, String sortOrder) {

SQLiteQueryBuilder qBuilder=new SQLiteQueryBuilder();

switch(sUriMatcher.match(uri)){

case EMPLOYEE:

qBuilder.setTables(DBHelper.EMPLOYEES_TABLE_NAME);

qBuilder.setProjectionMap(empProjectionMap);

break;

case EMPLOYEE_ID:

qBuilder.setTables(DBHelper.EMPLOYEES_TABLE_NAME);

qBuilder.setProjectionMap(empProjectionMap);

qBuilder.appendWhere(Employee._ID+"="+uri.getPathSegments().get(1));

break;

default:

throw new IllegalArgumentException("错误的URI"+uri);

}

String orderby;

if(TextUtils.isEmpty(sortOrder)){

orderby=Employee.DEFAULT_SORT_ORDER;

}else{

orderby=sortOrder;

}

SQLiteDatabase dbDatabase=dbHelper.getReadableDatabase();

Cursor cursor=qBuilder.query(dbDatabase, projection, selection, selectionArgs, null, null, orderby);

cursor.setNotificationUri(getContext().getContentResolver(), uri);

return cursor;

}

@Override

public String getType(Uri uri) {

return null;

}

@Override

public Uri insert(Uri uri, ContentValues values) {

SQLiteDatabase db=dbHelper.getWritableDatabase();

long rowId=db.insert(DBHelper.EMPLOYEES_TABLE_NAME, https://www.doczj.com/doc/cb1454757.html,, values);

if(rowId>0){

Uri empUri=ContentUris.withAppendedId(Employees.Employee.CONTENT_URI, rowId);

getContext().getContentResolver().notifyChange(empUri, null);

return empUri;

}

return null;

}

@Override

public int delete(Uri uri, String selection, String[] selectionArgs) {

SQLiteDatabase db=dbHelper.getWritableDatabase();

int count;

switch(sUriMatcher.match(uri)){

case EMPLOYEE:

count=db.delete(DBHelper.EMPLOYEES_TABLE_NAME, selection, selectionArgs);

break;

case EMPLOYEE_ID:

String noteId=uri.getPathSegments().get(1);

count=db.delete(DBHelper.EMPLOYEES_TABLE_NAME,

Employee._ID+"="+noteId+(!TextUtils.isEmpty(selection)?"AND("+selection+')':""), selectionArgs);

break;

default:

throw new IllegalArgumentException("错误的URI"+uri);

}

getContext().getContentResolver().notifyChange(uri, null);

return count;

}

@Override

public int update(Uri uri, ContentValues values, String selection,

String[] selectionArgs) {

SQLiteDatabase dbDatabase=dbHelper.getWritableDatabase();

int count;

Log.i("zhangss", sUriMatcher.match(uri)+"");

switch (sUriMatcher.match(uri)) {

case EMPLOYEE:

count=dbDatabase.update(DBHelper.EMPLOYEES_TABLE_NAME, values, selection, selectionArgs);

break;

case EMPLOYEE_ID:

String noteId=uri.getPathSegments().get(1);

count=dbDatabase.update(DBHelper.EMPLOYEES_TABLE_NAME, values, Employee._ID+"="+noteId+(!TextUtils.isEmpty(selection)?"AND("+selection+')':""), selectionArgs);

break;

default:

throw new IllegalArgumentException("代牦的URI"+uri);

}

getContext().getContentResolver().notifyChange(uri, null);

return count;

}

}

android 自定义圆角头像以及使用declare-styleable进行配置属性解析

android 自定义圆角头像以及使用declare-styleable进行配置属性解析由于最新项目中正在检查UI是否与效果图匹配,结果关于联系人模块给的默认图片是四角稍带弧度的圆角,而我们截取的图片是正方形的,现在要给应用统一替换。应用中既用到大圆角头像(即整个头像是圆的)又用到四角稍带弧度的圆角头像,封装一下以便重用。以下直接见代码 [java] view plain copy 在CODE上查看代码片派生到我的代码片 package com.test.demo; import com.test.demo.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Shader.TileMode; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Parcelable; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.widget.ImageView; /** * 圆角imageview */ public class RoundImageView extends ImageView { private static final String TAG = "RoundImageView"; /** * 图片的类型,圆形or圆角 */ private int type; public static final int TYPE_CIRCLE = 0; public static final int TYPE_ROUND = 1; /** * 圆角大小的默认值

android studio 控件常用属性

android studio 控件常用属性 下面是RelativeLayout各个属性 1.android:layout_above="@id/xxx" --将控件置于给定ID控件之上 2.android:layout_below="@id/xxx" --将控件置于给定ID控件之下 3. android:layout_toLeftOf="@id/xxx" --将控件的右边缘和给定ID控件的左边缘对齐 4.android:layout_toRightOf="@id/xxx" --将控件的左边缘和给定ID控件的右边缘对齐 5. android:layout_alignLeft="@id/xxx" --将控件的左边缘和给定ID控件的左边缘对齐 6.android:layout_alignTop="@id/xxx" --将控件的上边缘和给定ID控件的上边缘对齐 7.android:layout_alignRight="@id/xxx" --将控件的右边缘和给定ID控件的右边缘对齐 8.android:layout_alignBottom="@id/xxx" --将控件的底边缘和给定ID控件的底边缘对齐 9.android:layout_alignParentLeft="true" --将控件的左边缘和父控件的左边缘对齐 10. android:layout_alignParentTop="true" --将控件的上边缘和父控件的上边缘对齐 11. android:layout_alignParentRight="true" --将控件的右边缘和父控件的右边缘对齐 12.android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐 13.android:layout_centerInParent="true" --将控件置于父控件的中心位置 14.android:layout_centerHorizontal="true" --将控件置于水平方向的中心位置 15.android:layout_centerVertical="true" --将控件置于垂直方向的中心位置 android:layout_width 设置组件的宽度 android:layout_height 设置组件的高度 android:id 给组件定义一个id值,供后期使用 android:background 设置组件的背景颜色或背景图片 android:text 设置组件的显示文字 android:textColor 设置组件的显示文字的颜色 android:layout_below 组件在参考组件的下面 android:alignTop 同指定组件的顶平行

Android平台我的日记设计文档

Android平台我的日记 设计文档 项目名称:mydiray 项目结构示意: 阶段任务名称(一)布局的设计 开始时间: 结束时间: 设计者: 梁凌旭 一、本次任务完成的功能 1、各控件的显示 二、最终功能及效果 三、涉及知识点介绍 四、代码设计 activity_main.xml:

android:layout_centerHorizontal="true" android:layout_marginTop="88dp" android:text="@string/wo" android:textSize="35sp"/>

相关文档 最新文档