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


这个类首先在构造函数里面获得应用程序上下文的接口,然后通过就可以通过这个接口来访问中的文章信息了 。成员函数、和分别用来新增、更新和删除一个文章信息条目;成员函数用来获取所有的文章信息;成员函数和分别根据文章的ID和位置来获得具体文章信息条目;成员函数直接使用的未公开接口call来获得文章信息条目的数量,注意,这个函数要源代码环境下编译才能通过 。
下面是程序主界面类的实现,它实现在src/shy/luo//.java文件中:
package shy.luo.article;import shy.luo.providers.articles.Articles;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.database.ContentObserver;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {private final static String LOG_TAG = "shy.luo.article.MainActivity";private final static int ADD_ARTICAL_ACTIVITY = 1;private final static int EDIT_ARTICAL_ACTIVITY = 2;private ArticlesAdapter aa = null;private ArticleAdapter adapter = null;private ArticleObserver observer = null;private ListView articleList = null;private Button addButton = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);aa = new ArticlesAdapter(this);articleList = (ListView)findViewById(R.id.listview_article);adapter = new ArticleAdapter(this);articleList.setAdapter(adapter);articleList.setOnItemClickListener(this);observer = new ArticleObserver(new Handler());getContentResolver().registerContentObserver(Articles.CONTENT_URI, true, observer);addButton = (Button)findViewById(R.id.button_add);addButton.setOnClickListener(this);Log.i(LOG_TAG, "MainActivity Created");}@Overridepublic void onDestroy() {super.onDestroy();getContentResolver().unregisterContentObserver(observer);}@Overridepublic void onClick(View v) {if(v.equals(addButton)) {Intent intent = new Intent(this, ArticleActivity.class);startActivityForResult(intent, ADD_ARTICAL_ACTIVITY);}}@Overridepublic void onItemClick(AdapterView parent, View view, int pos, long id) {Intent intent = new Intent(this, ArticleActivity.class);Article article = aa.getArticleByPos(pos);intent.putExtra(Articles.ID, article.getId());intent.putExtra(Articles.TITLE, article.getTitle());intent.putExtra(Articles.ABSTRACT, article.getAbstract());intent.putExtra(Articles.URL, article.getUrl());startActivityForResult(intent, EDIT_ARTICAL_ACTIVITY);}@Overridepublic void onActivityResult(int requestCode,int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);switch(requestCode) {case ADD_ARTICAL_ACTIVITY: {if(resultCode == Activity.RESULT_OK) {String title = data.getStringExtra(Articles.TITLE);String abs = data.getStringExtra(Articles.ABSTRACT);String url = data.getStringExtra(Articles.URL);Article article = new Article(-1, title, abs, url);aa.insertArticle(article);}break;}case EDIT_ARTICAL_ACTIVITY: {if(resultCode == Activity.RESULT_OK) {int action = data.getIntExtra(ArticleActivity.EDIT_ARTICLE_ACTION, -1);if(action == ArticleActivity.MODIFY_ARTICLE) {int id = data.getIntExtra(Articles.ID, -1);String title = data.getStringExtra(Articles.TITLE);String abs = data.getStringExtra(Articles.ABSTRACT);String url = data.getStringExtra(Articles.URL);Article article = new Article(id, title, abs, url);aa.updateArticle(article);} else if(action == ArticleActivity.DELETE_ARTICLE){int id = data.getIntExtra(Articles.ID, -1);aa.removeArticle(id);}}break;}}}private class ArticleObserver extends ContentObserver {public ArticleObserver(Handler handler) {super(handler);}@Overridepublic void onChange (boolean selfChange) {adapter.notifyDataSetChanged();}}private class ArticleAdapter extends BaseAdapter {private LayoutInflater inflater;public ArticleAdapter(Context context){inflater = LayoutInflater.from(context);}@Overridepublic int getCount() {return aa.getArticleCount();}@Overridepublic Object getItem(int pos) {return aa.getArticleByPos(pos);}@Overridepublic long getItemId(int pos) {return aa.getArticleByPos(pos).getId();}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Article article = (Article)getItem(position);if (convertView == null) {convertView = inflater.inflate(R.layout.item, null);}TextView titleView = (TextView)convertView.findViewById(R.id.textview_article_title);titleView.setText("Title: " + article.getTitle());TextView abstractView = (TextView)convertView.findViewById(R.id.textview_article_abstract);abstractView.setText("Abstract: " + article.getAbstract());TextView urlView = (TextView)convertView.findViewById(R.id.textview_article_url);urlView.setText("URL: " + article.getUrl());return convertView;}}}