Android应用程序组件Content Provider应用实例( 七 )


这个应用程序的主界面包含了一个控件,用来显示从中得到的文章信息条目,在这个主界面上,可以浏览、增加、删除和更新文章信息 。当需要增加、删除或者更新文章信息时,就会跳到另外一个界面中去执行具体的操作 。为了方便开发,我们把每一个文章信息条目封装成了一个类,并且把与进交互的操作都通过类来实现 。下面介绍每一个类的具本实现 。
下面是类的实现,它实现在src/shy/luo/.java文件中:
package shy.luo.article;public class Article {private int id;private String title;private String abs;private String url;public Article(int id, String title, String abs, String url) {this.id = id;this.title = title;this.abs = abs;this.url = url;}public void setId(int id) {this.id = id;}public int getId() {return this.id;}public void setTitle(String title) {this.title = title;}public String getTitle() {return this.title;}public void setAbstract(String abs) {this.abs = abs;}public String getAbstract() {return this.abs;}public void setUrl(String url) {this.url = url;}public String getUrl() {return this.url;}}
下面是类的实现,它实现在src/shy/luo/.java文件中:
package shy.luo.article;import java.util.LinkedList;import shy.luo.providers.articles.Articles;import android.content.ContentResolver;import android.content.ContentUris;import android.content.ContentValues;import android.content.Context;import android.content.IContentProvider;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;import android.os.RemoteException;import android.util.Log;public class ArticlesAdapter {private static final String LOG_TAG = "shy.luo.article.ArticlesAdapter";private ContentResolver resolver = null;public ArticlesAdapter(Context context) {resolver = context.getContentResolver();}public long insertArticle(Article article) {ContentValues values = new ContentValues();values.put(Articles.TITLE, article.getTitle());values.put(Articles.ABSTRACT, article.getAbstract());values.put(Articles.URL, article.getUrl());Uri uri = resolver.insert(Articles.CONTENT_URI, values);String itemId = uri.getPathSegments().get(1);return Integer.valueOf(itemId).longValue();}public boolean updateArticle(Article article) {Uri uri = ContentUris.withAppendedId(Articles.CONTENT_URI, article.getId());ContentValues values = new ContentValues();values.put(Articles.TITLE, article.getTitle());values.put(Articles.ABSTRACT, article.getAbstract());values.put(Articles.URL, article.getUrl());int count = resolver.update(uri, values, null, null);return count > 0;}public boolean removeArticle(int id) {Uri uri = ContentUris.withAppendedId(Articles.CONTENT_URI, id);int count = resolver.delete(uri, null, null);return count > 0;}public LinkedList getAllArticles() {LinkedList articles = new LinkedList();String[] projection = new String[] {Articles.ID,Articles.TITLE,Articles.ABSTRACT,Articles.URL};Cursor cursor = resolver.query(Articles.CONTENT_URI, projection, null, null, Articles.DEFAULT_SORT_ORDER);if (cursor.moveToFirst()) {do {int id = cursor.getInt(0);String title = cursor.getString(1);String abs = cursor.getString(2);String url = cursor.getString(3);Article article = new Article(id, title, abs, url);articles.add(article);} while(cursor.moveToNext());}return articles;}public int getArticleCount() {int count = 0;try {IContentProvider provider = resolver.acquireProvider(Articles.CONTENT_URI);Bundle bundle = provider.call(Articles.METHOD_GET_ITEM_COUNT, null, null);count = bundle.getInt(Articles.KEY_ITEM_COUNT, 0);} catch(RemoteException e) {e.printStackTrace();}return count;}public Article getArticleById(int id) {Uri uri = ContentUris.withAppendedId(Articles.CONTENT_URI, id);String[] projection = new String[] {Articles.ID,Articles.TITLE,Articles.ABSTRACT,Articles.URL};Cursor cursor = resolver.query(uri, projection, null, null, Articles.DEFAULT_SORT_ORDER);Log.i(LOG_TAG, "cursor.moveToFirst");if (!cursor.moveToFirst()) {return null;}String title = cursor.getString(1);String abs = cursor.getString(2);String url = cursor.getString(3);return new Article(id, title, abs, url);}public Article getArticleByPos(int pos) {Uri uri = ContentUris.withAppendedId(Articles.CONTENT_POS_URI, pos);String[] projection = new String[] {Articles.ID,Articles.TITLE,Articles.ABSTRACT,Articles.URL};Cursor cursor = resolver.query(uri, projection, null, null, Articles.DEFAULT_SORT_ORDER);if (!cursor.moveToFirst()) {return null;}int id = cursor.getInt(0);String title = cursor.getString(1);String abs = cursor.getString(2);String url = cursor.getString(3);return new Article(id, title, abs, url);}}