zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android 第三次作业 contentprovider与resolver

Android 作业 第三次 contentprovider
2023-06-13 09:13:24 时间

作业要求:

1、contentprovider是安卓四大组件之一,请使用其方法类进行数据获取;

2、请自建一个provider,然后在另一个app中使用resolver调用这个provider。

3、本次作业请启用新项目,理论上需要两个APP进行实验。

首先我们新建两个新的项目,分别命名为ContentProvider和Myresolver。

我们先完成Myresolver的编写:在MainActivity.java里面写:

package com.example.myresolver;

import androidx.annotation.ContentView;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;

import android.content.ContentValues;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

public class MainActivity extends AppCompatActivity {

private Button button;

private ContentResolver resolver;

//private static final String AUTHORITY="hjy.Provider1";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button=findViewById(R.id.button);

ContentResolver resolver=getContentResolver();

Uri uri=Uri.parse("content://hjy.provider1/student");

ContentValues values=new ContentValues();

values.put("name","hjy");

values.put("age",19);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

resolver.insert(uri,values);

}

});

}

}

设计的resolver界面如下:

然后我们开始编写ContentProvider:

 先编写数据库有关代码,新建一个DatabaseActivity和一个MyDBhelper,代码如下:

package com.example.contentprovider;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;

import android.content.ContentValues;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

public class DatabaseActivity2 extends AppCompatActivity {

private Button button,button2,button3,button4;

private MyDBhelper dBhelper;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_database2);

button=findViewById(R.id.button);

button2=findViewById(R.id.button2);

button3=findViewById(R.id.button3);

button4=findViewById(R.id.button4);

MyDBhelper dBhelper=new MyDBhelper(this,"hjyDB",null,1);

SQLiteDatabase database=dBhelper.getReadableDatabase();

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

ContentValues values1=new ContentValues();

values1.put("name","zhangsan");

values1.put("age",19);

ContentValues values2=new ContentValues();

values2.put("name","lisi");

values2.put("age",20);

ContentValues values3=new ContentValues();

values3.put("name","wangwu");

values3.put("age",21);

ContentValues values4=new ContentValues();

values4.put("name","liuliu");

values4.put("age",21);

database.insert("student",null,values1);

database.insert("student",null,values2);

database.insert("student",null,values3);

database.insert("student",null,values4);

ContentValues values=new ContentValues();

}

});

button2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

ContentValues values=new ContentValues();

values.put("age",19);

database.update("student",values,"name=?",new String[]{"liuliu"});//修改liuliu的年龄为19

}

});

button3.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

// database.execSQL("delete from student");//删除表的所有内容

database.delete("student","name=?",new String[]{"liuliu"});//删除名字为liuliu的一行

}

});

button4.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Cursor cursor1=database.query("student",new String[]{"name"},"name=?",new String[]{"wangwu"},

null,null,null);

Cursor cursor2=database.rawQuery("select * from student where name=?",new String[]{"zhangsan"});//另一种查询方法

Log.d("hjy",String.valueOf(cursor2.getCount()));

Log.d("hjy","cursor2.getPosition:"+cursor2.getPosition());

while (cursor2.moveToNext()){

@SuppressLint("Range") Integer id=cursor2.getInt(cursor2.getColumnIndex("id"));

@SuppressLint("Range") String name=cursor2.getString(cursor2.getColumnIndex("name"));

Log.d("hjy","result"+id+name);

}

cursor2.close();

}

});

}

}

package com.example.contentprovider;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class MyDBhelper extends SQLiteOpenHelper {

public MyDBhelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory,int version){

super(context,"hjyDB",factory,version);}

@Override

public void onCreate(SQLiteDatabase sqLiteDatabase){

sqLiteDatabase.execSQL("create table student("+"id integer primary key autoincrement,name varchar(20),age interger)");

}

@Override

public void onUpgrade(SQLiteDatabase sqLiteDatabase,int i,int i1){}

}

DatabaseActivity的界面如下:

然后我们编写ContentProvider.java:

package com.example.contentprovider;

import android.content.ContentProvider;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.net.Uri;

public class MyContentProvider extends ContentProvider {

private MyDAO myDAO;

private Context context;

@Override

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

// Implement this to handle requests to delete one or more rows.

throw new UnsupportedOperationException("Not yet implemented");

}

@Override

public String getType(Uri uri) {

// TODO: Implement this to handle requests for the MIME type of the data

// at the given URI.

throw new UnsupportedOperationException("Not yet implemented");

}

@Override

public Uri insert(Uri uri, ContentValues values) {

// TODO: Implement this to handle requests to insert a new row.

//throw new UnsupportedOperationException("Not yet implemented");

return myDAO.hjyInsert(values);

}

@Override

public boolean onCreate() {

// TODO: Implement this to initialize your content provider on startup.

context=this.getContext();

myDAO=new MyDAO(context);

return true;

}

@Override

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

String[] selectionArgs, String sortOrder) {

// TODO: Implement this to handle query requests from clients.

throw new UnsupportedOperationException("Not yet implemented");

}

@Override

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

String[] selectionArgs) {

// TODO: Implement this to handle requests to update one or more rows.

throw new UnsupportedOperationException("Not yet implemented");

}

}

MyDao.java:

package com.example.contentprovider;

import android.content.ContentUris;

import android.content.ContentValues;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.net.Uri;

public class MyDAO {

private Context context;

private Uri uri=Uri.parse("content://hjy.provider1");

public MyDAO(Context context){

this.context=context;

}

// MyDBhelper dBhelper=new MyDBhelper(context,"hjyDB",null,1);

// SQLiteDatabase database=dBhelper.getWritableDatabase();

public Uri hjyInsert(ContentValues values){

MyDBhelper dBhelper=new MyDBhelper(context,"hjyDB",null,1);

SQLiteDatabase database=dBhelper.getWritableDatabase();

ContentValues values1=new ContentValues();

values1.put("name","zhangsan");

values1.put("age",19);

Uri uri=Uri.parse("content://hjy.provider1/student");

long rowId= database.insert("student",null,values1);

Uri inserturi= ContentUris.withAppendedId(uri,rowId);

return inserturi;

}

}

当然我们还要修改Mainfest里的内容,首先要增加permission

修改provider里的内容

以上就是代码编写的内容,然后我们运行contentprovider和resolver

运行结果:

点击INSETR按钮

源代码地址张浪浪的仓库: ......dfafa (gitee.com)