contentProvider

package veryedu.cls9.lession8.contentprovider;


import veryedu.lession8.database2.BookDao;
import veryedu.lession8.database2.entity.Book;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;


import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;


public class MainActivity extends Activity {

EditText  bookIdTxt;
EditText  bookTxt;
EditText  priceTxt;
EditText  authorTxt;
ListView  list1=null;
BookDao  bookDao=null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bookIdTxt=(EditText)findViewById(R.id.bookIdTxt);
        bookTxt=(EditText)findViewById(R.id.bookTxt);
        priceTxt=(EditText)findViewById(R.id.priceTxt);
        authorTxt=(EditText)findViewById(R.id.authorTxt);
        list1=(ListView)findViewById(R.id.list1);
    }
    
    public  void  clickBtn(View view)
    {
    int id=view.getId();
   
    switch(id)
    {
          case   R.id.btn1://保存
          Book book=new Book();
          book.setBookname(bookTxt.getText().toString());
          book.setBookprice(Float.parseFloat(priceTxt.getText().toString()));
               book.setAuthor(authorTxt.getText().toString());
          bookDao.insertBook(book);
       break;
          case   R.id.btn2://查询图书列表
         Log.i("test", "aaaaa");
           Cursor c= bookDao.getBookList();
           Log.i("test", "c.count="+c.getCount());
           String[] from={"bookname","bookprice","author"};
           int[]   to={R.id.bookLbl,R.id.priceLbl,R.id.authorLbl};
           SimpleCursorAdapter adapter=
            new SimpleCursorAdapter(this, R.layout.listitem, c, from, to);
           list1.setAdapter(adapter);
       break;
          case   R.id.btn3://根据ID查询
       break;
          case   R.id.btn4://根据ID删除
       break;
          case   R.id.btn5://更新
       break;
    }
    }




    @Override
protected void onPause() {
// TODO Auto-generated method stub
    bookDao.close();
super.onPause();
}








@Override
protected void onResume() {
// TODO Auto-generated method stub
bookDao=new BookDao(this);
bookDao.open();
super.onResume();
}








@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    

}



package veryedu.cls9.lession8.contentprovider;


public class BookDBMetaData {

public  static  final String  AUTHORITY="veryedu.cls9.lession8.provider.mybookprovider";

public static final String  DBNAME="book.db";
public static final int  VERSION=1;
public static final String  TBL_BOOK="tbl_book";

public static final class  TblBookMetaDate
{
public static final String  TBL_BOOK="tbl_book";
public static final String  _ID="_id";
public static final String  BOOK_NAME="bookname";
public static final String  BOOK_PRICE="bookprice";
public static final String  AUTHOR="author";
public static final String BOOKS_TYPE="vnd.android.cursor.dir/vnd.veryedu.books";
public static final String BOOK_TYPE="vnd.android.cursor.item/vnd.veryedu.books";
public  static final String  BOOK_URI="content://"+AUTHORITY+"/book";
}


}


package veryedu.cls9.lession8.contentprovider;


import org.apache.http.client.utils.URIUtils;


import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.net.Uri;
import android.text.TextUtils;


public class MyBookProvider extends ContentProvider {
static UriMatcher  uriMatcher;
public static final int BOOKS_CODE=1;
public static final int BOOK_CODE=2;
MyBookDBHelper  dbHelper;
SQLiteDatabase  db=null;

static{
uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
//当前的Provider可以接收如下的两个请求地址
//第一个请求地址编号是1 content://veryedu.cls9.lession8.provider.mybookprovider/book 
//这种地址是想要获得多条图书的信息
uriMatcher.addURI(BookDBMetaData.AUTHORITY, "book", 1);
//第2个请求地址编号是2 content://veryedu.cls9.lession8.provider.mybookprovider/book/13
//想要对book表中id编号是13的记录做操作
uriMatcher.addURI(BookDBMetaData.AUTHORITY, "book/#", 2);

}







@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}


@Override
public String getType(Uri uri) {
String type="";
switch(uriMatcher.match(uri))
{
case   BOOKS_CODE:
type=BookDBMetaData.TblBookMetaDate.BOOKS_TYPE;
break;
case   BOOK_CODE:
type=BookDBMetaData.TblBookMetaDate.BOOK_TYPE;
    break;
default:
throw new IllegalArgumentException("非法的URI地址");
}
return type;
}


@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
db=dbHelper.getWritableDatabase();
switch(uriMatcher.match(uri))
{
 case  BOOKS_CODE:
  long id= db.insert(BookDBMetaData.TblBookMetaDate.TBL_BOOK,
 BookDBMetaData.TblBookMetaDate.AUTHOR, 
 values)  ;
  //通知所有对当前数据感兴趣的Observer观察者数据已发生变动
  this.getContext().getContentResolver().notifyChange(uri, null);
  //拼接出新的URI地址并返回
   String uriStr=BookDBMetaData.TblBookMetaDate.BOOK_URI+"/"+id;
   uri=Uri.parse(uriStr);
   break;
     
 default :
    throw new IllegalArgumentException();
}
return uri;
}


@Override
public boolean onCreate() {
// TODO Auto-generated method stub
dbHelper=new MyBookDBHelper(this.getContext(), BookDBMetaData.DBNAME, null, BookDBMetaData.VERSION);
return false;
}


@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
db=dbHelper.getReadableDatabase();
Cursor c=null;
switch(uriMatcher.match(uri))
{
  
  // 单条地址:content://xxxxx/book/5
  case BOOK_CODE:
  long id=ContentUris.parseId(uri);
     String where=BookDBMetaData.TblBookMetaDate._ID +"="+id;
     if(selection!=null && selectionArgs!=null)
     {
     where= where+" and "+selection;
     }
   c=  db.query(BookDBMetaData.TBL_BOOK, projection, where, selectionArgs, null, null, sortOrder);
  break;
          // 多条地址:content://xxxxx/book/
  case BOOKS_CODE:
   c= db.query(BookDBMetaData.TBL_BOOK, projection, selection, selectionArgs, null, null, sortOrder);
  break;
}
return c;
}


@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}



private  class MyBookDBHelper  extends SQLiteOpenHelper
{


public MyBookDBHelper(Context context, String name,
CursorFactory factory, int version) {
//version是我们应用程序的版本号
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}


//当应用程序在手机中第一次执行时,会调用该方法创建数据库表
//以后的执行中就不会调用该方法了
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql="create  table  tbl_book (_id integer  primary key autoincrement,bookname,bookprice,author)";
db.execSQL(sql);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 升级  先删除原有的老表  然后按照新的结构重新建表
db.execSQL("drop tbl_book");
onCreate(db) ;
}
}


}

你可能感兴趣的:(contentProvider)