首页 > 编程知识 正文

android的四大组件有哪些,android与服务器端数据交互

时间:2023-05-04 00:42:11 阅读:111233 作者:1121

uri格式:

content://包名称/数据表名称/id/字段

有两个重要的方法:

ContentUris.parseId (;

contentUris.withappendedid(uri,id );

自定义内容提供程序具体包括:

通过ContentProvider向其他APP应用程序提供数据以实现数据共享。 其本质是通过调用本地APP应用程序数据库的具体操作,通过一个域名为其他APP应用程序提供访问该APP应用程序的数据库

步骤如下。

1、定义内容提供者的子类。

1.1第一个数据库工具类SQLiteDatabase的子类用于管理此APP的数据库。 privateDBHelperhelper=null;

1.2定义uri匹配类的默认不匹配

//uri定义匹配类,缺省情况下使其不匹配

privatestaticfinalurimatcheruri _ matcher=new uri matcher (uri matcher.no _ match );

1 .定义是访问三条记录还是访问多条记录

privatestaticfinalintSTUDENT=1; //单一记录

privatestaticfinalintstudents=2; //多条记录

1 .使用4个静态块定义匹配规则

//定义匹配规则的静态块

静态{

uri _ matcher.adduri (com.yqq.my content provider2. db.student provider )、student )、STUDENTS );

//标志位#单个记录uri _ matcher.adduri (com.yqq.my content provider2. db.student provider )、' student/# '、student

}

1.5匹配规则复盖六种方法

@Override

公共字符串获取类型(uri uri ) {

//如果一致成功则返回一致规则的codes,如果失败则返回-1

intflag=uri_matcher.match(uri );

是交换机(标志)

{

caseSTUDENT:

return ' vnd.Android.cursor.item/student ';

caseSTUDENTS:

return ' vnd.Android.cursor.dir/student ';

}

返回空值;

}

@Override

publicintdelete(uriuri,StringwhereSelection,String[]selectinArgs ) {

intcount=-1; //影响数据库的行数

try{

//获得匹配的uri和执行数据库

intflag=uri_matcher.match(uri );

sqlitedatabasedatabase=helper.getwritabledatabase (;

//删除一条或多条记录

开关(flag ) {

获取caseSTUDENT:{//内容提供者的id号。

longid=contentUris.parseid(uri;

//获得滤波器值

StringwhereValue='stud_id=' id;

//判断过滤条件是空还是“”

if(whereselection )!=nullwhereSelection.equals (' ' ) {

执行SQLite数据库的出入语句

count=database.delete('student ',whereValue,selectinArgs );

}

}

布雷克;

caseSTUDENTS:

{

//删除多行记录

count=database.delete('student ',whereSelection,selectinArgs );

}

布雷克;

}

}catch(exceptione ) {

Log.i (内容提供者

}

返回计数;

}

@Override

publicuriinsert(uriuri,ContentValuesvalues ) {

//定义返回的Uri类

Uri resultUri=null;

//匹配当前传入的uri

int flag=URI_MATCHER.match(uri);

switch(flag)

{//插入多条记录

case STUDENTS:

//获得数据库

SQLiteDatabase database=helper.getWritableDatabase();

//插入数据获得插入数据的行号

long id=database.insert("student", null, values);

//通过新插入数据的行号来获取需要返回的Uri。

resultUri=ContentUris.withAppendedId(uri, id);

break;

}

Log.i(TAG,"<<<<<<<<<

return resultUri;

}

@Override

public boolean onCreate() {

//初始化一个数据库工具类子类

helper=new DBHelper(getContext());

return false;

}

@Override

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

String[] selectionArgs, String sortOrder) {

// 定义一个游标用于查询

Cursor cursor = null;

//

try{

SQLiteDatabase database = helper.getReadableDatabase();

// 进行URI匹配

int flag = URI_MATCHER.match(uri);

// 匹配后的查询选择

switch (flag) {

//单条记录

case STUDENT: {

//获得该记录的id

long id=ContentUris.parseId(uri);

String where_value="stud_id="+id;

if(selection!=null&&selection!="")

{

where_value+="and"+selection;

}

//SQLiteDatabase进行查询

cursor=database.query("student",null, where_value, selectionArgs, null, null, null,null);

} break;

case STUDENTS:

cursor=database.query("student", null, selection, selectionArgs, null,null,null,null);

}

}catch(Exception e){

Log.i("查询<<

}

return cursor;

}

@Override

public int update(Uri uri, ContentValues values, String whereClause, String[] whereArgs) {

//受影响的数据的行数

int count=-1;

//匹配URI

int flag=URI_MATCHER.match(uri);

//获得id号

long id=ContentUris.parseId(uri);

//获得过滤条件

String whereValue="stud_id="+id;

if(whereClause!=null&&whereClause.equals(""))

{

whereValue+="and" +whereClause;

}

//获得进行操作的数据库

SQLiteDatabase database=helper.getWritableDatabase();

switch(flag)

{

//单行记录

case STUDENT:

{

//update table student set name=?id=?,addr=?where id=?

count=database.update("student", values, whereValue, whereArgs);

}

}

return count;

}

测试类:

public class Test extends AndroidTestCase {

/*public void testInsert()

{

//1、获得一个内容解析者

ContentResolver contentResolver=getContext().getContentResolver();

//2获得URI清单文件中(包名加类名加标识符)(注意加上content://)

Uriuri=Uri.parse("content://com.yqq.mycontentprovider2.db.StudentProvider/student");

//获得一个类似map的集合对象

ContentValues values=new ContentValues();

//往该集合添加数据时要注意数据的类型

values.put("stud_name", "gxdxx");

values.put("stud_sex", "男");

values.put("stud_age", 25);

values.put("stud_phone", "13389009881");

values.put("stud_remark", "已毕业");

contentResolver.insert(uri, values);

Log.i("数据插入","数据出插入成功!!!");

}*/

/*public void testDelete()

{

//获得一个内容解析者

ContentResolver contentResolver=getContext().getContentResolver();

//获得Uri对象

//删除单条记录

//Uriuri=Uri.parse("content://com.yqq.mycontentprovider2.db.StudentProvider/student/1");

//获得Uri删除多条记录

Uriuri=Uri.parse("content://com.yqq.mycontentprovider2.db.StudentProvider/student");

intflag=contentResolver.delete(uri, null,null );

Log.i("数据删除","数据出删除成功!!!"+flag);

}*/

/*public void testUpdata()

{

//获得一个内容解析者

ContentResolver contentResolver=getContext().getContentResolver();

//获得URI

Uriuri=Uri.parse("content://com.yqq.mycontentprovider2.db.StudentProvider/student/7");

ContentValues values=new ContentValues();

values.put("stud_name", "黎明");

values.put("stud_sex", "女");

values.put("stud_age", 23);

values.put("stud_phone", "13789009881");

values.put("stud_remark", "本科在读");

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

Log.i("数据更新","数据出更新成功!!!");

}*/

public void testQue()

{

ContentResolver contentResolver=getContext().getContentResolver();

/*//获得URI单条记录

Uriuri=Uri.parse("content://com.yqq.mycontentprovider2.db.StudentProvider/student/7");

//获得URI多条条记录

Uriuri=Uri.parse("content://com.yqq.mycontentprovider2.db.StudentProvider/student");

*/

/*

Cursor cursor=contentResolver.query(uri, null, null, null, null);

intcolums=cursor.getColumnCount();

Mapmap=new HashMap();

while(cursor.moveToNext())

{

for(inti=0;i

{

String columName=cursor.getColumnName(i);

String columValue=cursor.getString(cursor.getColumnIndex(columName));

if(columValue==null)

{

columValue=" ";

}

map.put(columName, columValue);

}

}

Log.i("数据查找",map.toString());

*/

///

//多条记录查询

//获得URI多条条记录

Uri uri=Uri.parse("content://com.yqq.mycontentprovider2.db.StudentProvider/student");

Cursor cursor=contentResolver.query(uri, null, null, null, null);

int colums=cursor.getColumnCount();

List>list=new ArrayList>();

while(cursor.moveToNext())

{

Mapmap=new HashMap();

for(int i=0;i{

String columName=cursor.getColumnName(i);

String columValue=cursor.getString(cursor.getColumnIndex(columName));

if(columValue==null)

{

columValue=" ";

}

map.put(columName, columValue);

}

list.add(map);

}

Log.i("数据查找",list.toString());

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。