不同应用程序提供的数据的获取显示—自定义ContentProvider

来源:互联网 发布:域名访问升级中 编辑:程序博客网 时间:2024/05/16 15:21
provider 
002            android:name="com.course.day16course02.MyContentProvider"
003            android:authorities="com.course.day16course02.mycontentprovider"
004            android:exported="true">
005        provider
006              
007             
008 public class MySqliteHelper extends SQLiteOpenHelper {
009 
010    private static final String DBNAME="products.db";
011    private static final int VERSION = 1;
012    public MySqliteHelper(Context context) {
013        super(context, DBNAME, null, VERSION);
014        // TODO Auto-generated constructor stub
015    }
016 
017    @Override
018    public void onCreate(SQLiteDatabase db) {
019        // TODO Auto-generated method stub
020        String sql = "create table if not exists pro(_id integer primary key autoincrement,pname text,price text,provider text)";
021        db.execSQL(sql);
022        for(int i=0;i<</span>10;i++){
023            ContentValues cv = new ContentValues();
024            cv.put("pname", "andry"+i);
025            cv.put("price", "12"+i);
026            cv.put("provider", "联想"+i);
027            db.insert("pro", null, cv);
028        }
029    }
030 
031    @Override
032    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
033        if(newVersion>oldVersion){
034            db.execSQL("drop table pro");
035            onCreate(db);
036        }
037      
038 
039      
040 public class MyContentProvider extends ContentProvider {
041 
042    private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
043    private static String autority="com.course.day16course02.mycontentprovider";
044    private MySqliteHelper helper;
045    //添加Uri的匹配规则
046    static{
047        matcher.addURI(autority, "insert", 1);
048        matcher.addURI(autority, "query", 2);
049        matcher.addURI(autority, "update", 3);
050        matcher.addURI(autority, "delete", 4);
051    }
052    @Override
053    public boolean onCreate() {
054        // 创建数据库工具类对象
055        helper= new MySqliteHelper(getContext());
056        return false;
057    }
058 
059    @Override
060    public Cursor query(Uri uri, String[] projection, String selection,
061            String[] selectionArgs, String sortOrder) {
062        // 查找
063        Cursor cursor = null;
064        if(matcher.match(uri)==2){
065            SQLiteDatabase db = helper.getReadableDatabase();
066            cursor = db.query("pro", projection, selection, selectionArgs, null, null, sortOrder);
067        }else{
068            throw new IllegalArgumentException("查找数据出错,找不到正确的Uri"+uri.toString());
069        }
070        return cursor;
071    }
072 
073    @Override
074    public String getType(Uri uri) {
075        // TODO Auto-generated method stub
076        return null;
077    }
078 
079    @Override
080    public Uri insert(Uri uri, ContentValues values) {
081        // 增加
082        Uri newuri = null;
083        if(matcher.match(uri)==1){
084            SQLiteDatabase db = helper.getReadableDatabase();
085            long id = db.insert("pro", null, values);
086            newuri = ContentUris.withAppendedId(uri, id);
087        }else{
088            throw new IllegalArgumentException("添加数据出错,找不到正确的Uri"+uri.toString());
089        }
090        return newuri;
091    }
092 
093    @Override
094    public int delete(Uri uri, String selection, String[] selectionArgs) {
095        // 删除
096    int result = 0;
097    if(matcher.match(uri)==4){
098        SQLiteDatabase db = helper.getReadableDatabase();
099        result = db.delete("pro", selection, selectionArgs);
100    }else{
101        throw new IllegalArgumentException("删除数据出错,找不到正确的Uri"+uri.toString());
102    }
103        return result;
104    }
105 
106    @Override
107    public int update(Uri uri, ContentValues values, String selection,
108            String[] selectionArgs) {
109        // 修改
110        int result =0;
111        if(matcher.match(uri)==3){
112            SQLiteDatabase db = helper.getReadableDatabase();
113            result = db.update("pro", values, selection, selectionArgs);
114        }else{
115            throw new IllegalArgumentException("更新数据出错,找不到正确的Uri"+uri.toString());
116        }
117        return result;
118    }
119 
120   
121   public class MainActivity extends Activity {
122 
123    private ListView lv;
124    private SimpleCursorAdapter adapter;
125    private ContentResolver resolver;
126   private String queryuri="content://com.course.day16course02.mycontentprovider/query";
127   private String inserturi = "content://com.course.day16course02.mycontentprovider/insert";
128   private String updateuri="content://com.course.day16course02.mycontentprovider/update";
129   private String deleteuri = "content://com.course.day16course02.mycontentprovider/delete";
130    @Override
131    protected void onCreate(Bundle savedInstanceState) {
132        super.onCreate(savedInstanceState);
133        setContentView(R.layout.activity_main);
134        lv = (ListView) findViewById(R.id.lv);
135        resolver = getContentResolver();
136        Cursor cursor = resolver.query(Uri.parse(queryuri), null, null, null, null);
137        adapter = new SimpleCursorAdapter(this, R.layout.listview, cursor, new String[]{"_id","pname","price","provider"}, new int[]{R.id.tvid,R.id.tvname,R.id.tvprice,R.id.tvprovider},SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
138        lv.setAdapter(adapter);
139    }
140    public void insert(View view){
141        ContentValues cv =null;
142         cv = new ContentValues();
143        cv.put("pname", "张三");
144        cv.put("price", "1231");
145        cv.put("provider","联想");
146        resolver.insert(Uri.parse(inserturi), cv);
147        cv = new ContentValues();
148        cv.put("pname", "李四");
149        cv.put("price", "1231");
150        cv.put("provider","联想");
151        resolver.insert(Uri.parse(inserturi), cv);
152        cv = new ContentValues();
153        cv.put("pname", "王二麻子");
154        cv.put("price", "1231");
155        cv.put("provider","联想");
156        resolver.insert(Uri.parse(inserturi), cv);
157        cv = new ContentValues();
158        cv.put("pname", "小淘气");
159        cv.put("price", "1231");
160        cv.put("provider","联想");
161        resolver.insert(Uri.parse(inserturi), cv);
162        cv = new ContentValues();
163        cv.put("pname", "绿鲤鱼");
164        cv.put("price", "1231");
165        cv.put("provider","联想");
166        resolver.insert(Uri.parse(inserturi), cv);
167        cv = new ContentValues();
168        cv.put("pname", "红鲤鱼");
169        cv.put("price", "1231");
170        cv.put("provider","联想");
171        resolver.insert(Uri.parse(inserturi), cv);
172        cv = new ContentValues();
173        cv.put("pname", "驴");
174        cv.put("price", "1231");
175        cv.put("provider","联想");
176        resolver.insert(Uri.parse(inserturi), cv);
177    }
178    public void delete(View view){
179        int result =  resolver.delete(Uri.parse(deleteuri), "_id=?", new String[]{"4"});
180        if(result>0){
181            Toast.makeText(this, "删除成功", Toast.LENGTH_LONG).show();
182        }
183    }
184    public void update(View view){
185        ContentValues cv =null;
186        cv = new ContentValues();
187       cv.put("pname", "张小贝");
188       cv.put("price", "1231");
189       cv.put("provider","CIA");
190 
191        int result = resolver.update(Uri.parse(updateuri), cv, "_id=?", new String[]{"2"});
192        if(result>0){
193            Toast.makeText(this, "修改成功", Toast.LENGTH_LONG).show();
194        }
195    }
0 0
原创粉丝点击