移动开发作业三

作业要求:

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按钮

仓库地址:as1-master: android3作业

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: MIT移动开发技术大作业是指麻省理工学院移动开发技术课程的大作业。该课程旨在教授学生移动应用开发的基本知识和技巧,使他们能够开发出功能完善、用户友好的移动应用程序。 在大作业中,学生需要通过结合课程所学的理论知识和实践技能,选择一个具有挑战性的移动应用项目来完成。他们需要分析目标用户群体的需求,设计应用界面,实现核心功能,进行用户测试和反馈收集,并最终提交一个完整的移动应用项目。 大作业的评估标准主要包括功能完整性、用户友好性、创新性以及代码质量等方面。学生需要展示他们在移动应用开发过程中的技术能力和创造力,并能够有效地解决实际问题。 通过这个大作业,学生将能够巩固和应用所学的理论知识,提升移动应用开发的实践能力。此外,学生还能够通过与目标用户的交互和反馈收集,理解用户需求,改善和优化应用程序。 总之,MIT移动开发技术大作业是培养学生移动应用开发技能和创新能力的重要环节,通过该作业的完成,学生能够应对实际挑战,开发出符合用户需求的高质量移动应用程序。 ### 回答2: MIT移动开发技术大作业是一项针对学生而设计的任务,旨在提升学生在移动开发方面的技术能力和创新能力。在这个大作业中,学生通常被要求完成一个移动应用的开发,该应用可以是一个游戏、社交媒体应用、健康管理工具等。 完成MIT移动开发技术大作业需要学生具备一定的编程基础和移动开发相关知识。学生需要利用所学的编程语言和开发工具,例如Java、Swift、React Native等,来设计和开发一个功能完整且具有创新性的移动应用。在开发的过程中,学生需要理解移动应用的设计原则和用户体验,同时考虑应用的性能优化和安全性等方面。 大作业的评估标准通常包括应用的功能实现程度、界面设计与交互体验、代码结构与质量、创新性和实用性等。学生需要通过清晰的需求分析、合理的系统设计和高质量的编码来完成任务,并能进行有效的测试和调试。 这个大作业为学生提供了一个实践和展示自己技术能力的机会,同时也鼓励学生探索和创新。通过完成这个大作业,学生可以深入理解移动开发的流程和技术要点,提升自己的综合能力,并为以后的学习和就业打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值