Android:ContentProvider的基本方法以及ContentResolver的对Provider的简单增删改查
来源:互联网 发布:街头篮球淘宝买号封停 编辑:程序博客网 时间:2024/06/15 04:04
ContentProvider使我们Android四大组件之一,ContentProvider可以实现在应用程序之间共享数据.
下面是本人自己写的一个简单dome
当然也可以参考详细的博客,点击这里
目录
- 目录
- 代码块
代码块
首先是ContentProvider的代码:
public class MyPersonProvider extends ContentProvider { private SQLiteDatabase database; @Override public boolean onCreate() { database = new DbWrite().openDatabase(getContext()); return false; } @Nullable @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { return database.query(false,"myblacklist",new String[]{" bid as _id","bname","bnumber"},selection,selectionArgs,null,null,sortOrder,null); } @Nullable @Override public String getType(Uri uri) { return null; } @Nullable @Override public Uri insert(Uri uri, ContentValues values) { long id= database.insert("myblacklist","bname",values); //通知所有监听这个数据库的观察者,数据发生改变。 getContext().getContentResolver().notifyChange(uri,null); return ContentUris.withAppendedId(uri,id); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { return database.delete("myblacklist",selection,selectionArgs); } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return database.update("myblacklist",values,selection,selectionArgs); }}
Provider清单文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_22_servicesblacklist"> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".receiver.CallInterceptReceivec"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> </receiver> <activity android:name=".InterceptActivity"></activity> <provider android:authorities="com.example.android_22_servicesblacklist.BLACKLIST" android:exported="true" android:name=".provider.MyPersonProvider"> </provider> </application></manifest>
下面是内容访问者的代码:
public class MainActivity extends ListActivity { private ContentResolver resolver; private EditText name; private EditText number; private ListView listView; private PopupWindow popupWindow; private View popupView; private SimpleCursorAdapter simpleCursorAdapter; private Cursor cursor; private Uri uri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //得到布局文件中的listview listView = getListView(); //得到访问者 resolver = getContentResolver(); //初始化数据 getData(); //listView的长按监听事件 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, final View view, int position, long id) { //弹出窗口进行修改或者删除的操作 popupWindow.showAtLocation(findViewById(R.id.activity_main), Gravity.CENTER, 0, 0); TextView textView= (TextView) popupView.findViewById(R.id.tv_font_1);//修改 final TextView bid= (TextView) view.findViewById(R.id.tv_item_list_id);//得到当前选中的id final TextView name= (TextView) view.findViewById(R.id.tv_item_list_name);//名字 final TextView number= (TextView) view.findViewById(R.id.tv_item_list_number);//号码 //修改数据 textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //调用这个自定义的方法,执行不同操作 caoData(false,new MyblackList(Integer.parseInt(bid.getText().toString()),name.getText().toString(),number.getText().toString())); popupWindow.dismiss();//关闭窗口 } }); TextView textView2= (TextView) popupView.findViewById(R.id.tv_font_2);//删除 textView2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { resolver.delete(uri,"bid=?",new String[]{bid.getText().toString()}); cursor= resolver.query(uri,null,null,null,null); simpleCursorAdapter.changeCursor(cursor); popupWindow.dismiss();//关闭窗口 } }); return false; } }); } public void getData(){ //解析uri字符串拿到uri uri = Uri.parse("content://com.example.android_22_servicesblacklist.BLACKLIST"); //通过访问者查询数据返回给cursor cursor = resolver.query(uri,null,null,null,null); simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.item_listview, cursor,new String[]{"_id" ,"bname","bnumber"},new int[]{R.id.tv_item_list_id,R.id.tv_item_list_name,R.id.tv_item_list_number}); listView.setAdapter(simpleCursorAdapter);//设置适配器 //得到窗口的一个布局文件 popupView = getLayoutInflater().inflate(R.layout.layout_popupwindow, null); popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,true); popupWindow.setTouchable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(),(Bitmap)null)); popupWindow.getContentView().setFocusableInTouchMode(true); popupWindow.getContentView().setFocusable(true);// popupWindow.setAnimationStyle(R.style.anim_menu_bottombar);//显示的动画效果 popupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0 && event.getAction() == KeyEvent.ACTION_DOWN) { if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); } return true; } return false; } }); } public void addData(View view){ caoData(true,null); } /** * * @param isAdd 是否为添加,否则为修改 * @param myblackList 待修改的对象 */ public void caoData(final boolean isAdd, final MyblackList myblackList){ //添加或修改的窗口 View view1= getLayoutInflater().inflate(R.layout.layout_resolver,null); name = (EditText) view1.findViewById(R.id.et_name); number = (EditText) view1.findViewById(R.id.et_number); //判断是不是为空 ,为空就是添加操作 if(myblackList!=null){ name.setText(myblackList.getBname()); number.setText(myblackList.getBnumber()); } AlertDialog.Builder dialog=new AlertDialog.Builder(this); final AlertDialog dialog1=dialog.create(); dialog1.setTitle("信息"); dialog1.setView(view1); //显示窗口 dialog1.show(); //得到保存按钮 Button btn_save= (Button) view1.findViewById(R.id.btn_save); btn_save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(TextUtils.isEmpty(number.getText().toString())){ Toast.makeText(MainActivity.this, "不能为空!", Toast.LENGTH_SHORT).show(); }else{ ContentValues values=new ContentValues(); if(isAdd){//为添加窗口 values.put("bname", name.getText()+""); values.put("bnumber", number.getText()+""); resolver.insert(uri,values); }else{//为修改窗口 values.put("bname",name.getText()+""); values.put("bnumber",number.getText()+""); int result= resolver.update(uri,values,"bid=?",new String[]{myblackList.getBid()+""}); } cursor= resolver.query(uri,null,null,null,null); //通知适配器 cursor发生改变 simpleCursorAdapter.changeCursor(cursor); //关闭窗口 dialog1.dismiss(); } } }); //得到修改按钮 Button btn_no= (Button) view1.findViewById(R.id.btn_no); btn_no.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog1.dismiss(); } }); }}
layout_resolver.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:id="@+id/et_name" android:hint="名字" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:id="@+id/et_number" android:hint="号码" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_weight="1" android:id="@+id/btn_save" android:layout_height="wrap_content" android:text="保存" /> <Button android:layout_width="0dp" android:layout_weight="1" android:id="@+id/btn_no" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout></LinearLayout>
layout_popupwindow.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:background="#55cccccc" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:gravity="center" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="15dp" android:layout_marginBottom="15dp" android:text="修改信息" android:id="@+id/tv_font_1" android:textSize="14sp" /> <View android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#cccccc" > </View> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center_horizontal" android:layout_marginTop="15dp" android:layout_marginBottom="15dp" android:text="删除信息" android:id="@+id/tv_font_2" android:textSize="16sp" /> <View android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#cccccc" > </View></LinearLayout>
ContentResolver的界面,增加, listview展示,及popupwindow修改或删除
效果图如下:
源码下载地址:http://download.csdn.net/detail/james_lang/9748487
0 0
- Android:ContentProvider的基本方法以及ContentResolver的对Provider的简单增删改查
- android的与ContentResolver(内容访问者)对ContentProvider(内容提供者)的增删查改
- contentProvider简单实现数据库的增删改查
- Android 使用ContentProvider ContentResolver 实现增删改查
- ofbiz 简单的增删改查方法
- Android 对sqlite数据库增删查改的dao封装,以及事务的简单操作(简单银行转帐)
- 基本的增删改查
- 数据库的创建以及对数据库的增删改查
- 自定义ContentProvider的简单增删除改
- thinkPHP项目部署以及基本模型的增删改查
- 对Oracle的增删改查基本操作
- 简单的Contentprovider和contentResolver
- 关于JDBC 对数据库进行创建以及简单的增删改查操作
- MyBatis基本配置和简单的增删改查
- 关于SSM框架搭建以及简单的增删改查
- MyBatis对数据库的增删改查操作,简单示例
- MyBatis对数据库的增删改查操作,简单示例
- ContentProvider 的创建以及增删改查操作(你想看的都在这里了)
- bzoj 2820 莫比乌斯反演
- 数组越界那点事儿
- springmvc 拦截器
- QQ微信第三方登录
- 新手使用GitHub客户端提交本地项目到GitHub网站详细步骤
- Android:ContentProvider的基本方法以及ContentResolver的对Provider的简单增删改查
- html css层叠样式基础(选择器的优先级)(六)
- HTML5来了,7个混合式移动开发框架
- PAT BASIC 1009
- VIM 配色基本知识
- 富兰克林说世界上有3种人
- Excle也能编程-VBA小工具
- poj 2236 Wireless Network
- 蓝桥杯-第七届省赛javaA组-抽签