Android学习篇章37-ContentProvider-ContentResolver

MainActivity:

public class MainActivity extends Activity {
	EditText  bookIdTxt=null;
	EditText  bookTxt=null;
	EditText  authorTxt=null;
	EditText  priceTxt=null;
	ListView  listview1=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}
	@Override
	protected void onPause() {

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

		super.onStop();
	}
	public  void  clickBtn(View view)
	{
		  int id=view.getId();
		  if(id==R.id.btn1)
		  {
			  //增加图书信息
			  addBook();
		  }else if(id==R.id.btn2)
		  {
			  delBookById();
			  findAllBook3();
		  }else if(id==R.id.btn3)
		  {
			  updateBookById();
		  }else if(id==R.id.btn4)
		  {
			  findBookById();
		  }else if(id==R.id.btn5)
		  {
			  //findAllBookByCursor();
			  findAllBook3();
		  }
	}
	public void  delBookById()
	{
		String bookId=""+bookIdTxt.getText();

	}
	public void findAllBookByCursor()
	{
	}
	
	public void findAllBook()
	{
//		List<Book> bookList= db.queryAllBooks();
//		
//		List  list=new ArrayList<String>();
//		for(int i=0;i<bookList.size();i++)
//		{
//			list.add(""+bookList.get(i));
//		}
//		ArrayAdapter<String>  adapter=
//				new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
//		listview1.setAdapter(adapter);
		
	}
	
	
	public void findAllBook2()
	{
//		List<Book> bookList= db.queryAllBooks();
//		mybookadapter adapter=(mybookadapter)listview1.getAdapter();
//        if(adapter==null)
//        {
//		  adapter=new mybookadapter(this);
//		  listview1.setAdapter(adapter);
//        }
//		adapter.setData(bookList);
//		//要通知数据集发生变动
//		adapter.notifyDataSetChanged();
	}	
	public void findAllBook3()
	{
         Cursor c=this.getContentResolver().query(Uri.parse(TblBookinfoMetaData.BOOKS_URI), null, null, null, null);
         List<Map<String, Object>> mapList=new ArrayList<Map<String, Object>>();
         if(c!=null && c.moveToFirst())
         {
           do
		   {
			Map m=new HashMap<String, Object>();
			m.put("bookid", c.getInt(c.getColumnIndex("_id")));
			m.put("bookname",c.getString(c.getColumnIndex("bookname")));
			m.put("author", c.getString(c.getColumnIndex("author")));
			m.put("price", c.getFloat(c.getColumnIndex("price")));
			mapList.add(m);
		    }while(c.moveToNext());
         }
		String[] from={"bookid","bookname","author","price"};
		int[] to={R.id.bookIdTxt,R.id.bookTxt,R.id.authorTxt,R.id.priceTxt};
		SimpleAdapter adapter=new SimpleAdapter(this, mapList, R.layout.line, from, to);
		  listview1.setAdapter(adapter);
		//要通知数据集发生变动
		adapter.notifyDataSetChanged();
	}	
	public  void findBookById()
	{
	}
	public void addBook()
	{
		String name=""+bookTxt.getText();
		String author=""+authorTxt.getText();
		float price=Float.parseFloat(""+priceTxt.getText());
//		Intent intent=new Intent();
//		intent.setAction(Intent.ACTION_VIEW);
//		intent.setData(Uri.parse("content://veryedu.cls229.lession12.MyProvider/book/12"));
//        startActivity(intent);	
		ContentValues  values=new ContentValues();
		values.put("bookname", name);
		values.put("author",author);
		values.put("price", price);
		this.getContentResolver().insert(Uri.parse(TblBookinfoMetaData.BOOKS_URI), values);
	}

	public  void updateBookById()
	{
	}
	
	public class mybookadapter extends  BaseAdapter
	{
		List<Book> list=new ArrayList<Book>();
		LayoutInflater inflater=null;
		Context context;
		public  mybookadapter(Context context)
		{
			this.context=context;
			inflater=LayoutInflater.from(context);
		}
		public void setData(List<Book>  list)
		{
			this.list=list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			Book book=list.get(position);
			if(convertView==null)
			{
				convertView=inflater.inflate(R.layout.line,null);
				CompnentHolder ch=new CompnentHolder();
				ch.idTxt=(TextView)convertView.findViewById(R.id.bookIdTxt);
			    ch.nameTxt=(TextView)convertView.findViewById(R.id.bookTxt);
			    ch.authorTxt=(TextView)convertView.findViewById(R.id.authorTxt);
			    ch.priceTxt=(TextView)convertView.findViewById(R.id.priceTxt);
                convertView.setTag(ch);			    
			}
			
			CompnentHolder ch=(CompnentHolder)convertView.getTag();
			ch.idTxt.setText(""+book.bookId);
			ch.nameTxt.setText(book.bookname);
			ch.authorTxt.setText(book.author);
			ch.priceTxt.setText(""+book.price);
			
			
			if(position%2==0)
			{
			  convertView.setBackgroundColor(Color.RED);
			  ch.idTxt.setTextColor(Color.WHITE);
			  ch.nameTxt.setTextColor(Color.WHITE);
			  ch.authorTxt.setTextColor(Color.WHITE);
			  ch.priceTxt.setTextColor(Color.WHITE);
			}else
			{
			  convertView.setBackgroundColor(Color.WHITE);
			  ch.idTxt.setTextColor(Color.BLACK);
			  ch.nameTxt.setTextColor(Color.BLACK);
			  ch.authorTxt.setTextColor(Color.BLACK);
			  ch.priceTxt.setTextColor(Color.BLACK);			  
			}
			return convertView;
		}
	}
	public void init()
	{
		  bookIdTxt=(EditText)findViewById(R.id.bookIdTxt);
		  bookTxt=(EditText)findViewById(R.id.bookTxt);
		  authorTxt=(EditText)findViewById(R.id.authorTxt);
		  priceTxt=(EditText)findViewById(R.id.priceTxt);
		  listview1=(ListView)findViewById(R.id.listview1);
	}
	
	public class   CompnentHolder
	{
		public  TextView idTxt;
		public  TextView nameTxt;
		public  TextView authorTxt;
		public  TextView priceTxt;
	}
	@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;
	}

}

元数据类:

//book.db的元数据类
public class BookMetaData {
	public static final  String AUTHORITY="com.MyProvider";//ContentProvider的标识
	public static final  String  BOOK_DB="book.db";
	public static final  int   VISION=1;
	public static final  String  TBL_BOOK="bookinfo";
	public static class  TblBookinfoMetaData implements  BaseColumns
	{
		//访问多本图书的uri地址 content://com.MyProvider/book
		public  static final  String BOOKS_URI="content://"+AUTHORITY+"/book";
		//访问单本图书的uri地址 content://com.MyProvider/book/11
		public  static final  String BOOK_URI="content://"+AUTHORITY+"/book/";
		
		//多条数据的数据类型
		public  static final String BOOK_DIR_TYPE="vnd.android.cursor.dir/vnd.mybooks.contenttype";
		//单条数据的数据类型
		public  static final String BOOK_ITEM_TYPE="vnd.android.cursor.item/vnd.mybooks.contenttype";
		
		public  static final  String TBL_BOOK="bookinfo";
        public  static final  String BOOK_NAME="bookname";
        public  static final  String BOOK_AUTHOR="author";
        public  static final  String BOOK_PRICE="price";
	}
}

实体类:

public class Book {
	
	public int bookId;
	public String bookname;
	public String author;
	public float  price;
	
	public String toString()
	{
		return  "id:"+bookId+" 书名:"+bookname
				+" 作者:"+author+" 价格:"+price;
	}

}


Xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="图书编号" />
    <EditText  android:id="@+id/bookIdTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="书名" />
    <EditText  android:id="@+id/bookTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="作者" />
    <EditText  android:id="@+id/authorTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="价格" />
    <EditText  android:id="@+id/priceTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />    
    <Button android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="clickBtn"
        android:text="增加"
        />
    <Button android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="clickBtn"
        android:text="删除"
        />
    <Button android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="clickBtn"
        android:text="修改"
        />
    <Button android:id="@+id/btn4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="clickBtn"
        android:text="根据ID查询"
        />
    <Button android:id="@+id/btn5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="clickBtn"
        android:text="查询所有"
        />
    <ListView android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

line.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    
    android:orientation="horizontal" >
    <TextView android:id="@+id/bookIdTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="35sp"
        android:textColor="#f00"
        android:layout_marginRight="5dp"
        />

    <TextView android:id="@+id/bookTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="35sp"
        android:textColor="#000"
        android:layout_marginRight="5dp"
        />
    <TextView android:id="@+id/authorTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="35sp"
        android:textColor="#f00"
        android:layout_marginRight="5dp"
        />
    <TextView android:id="@+id/priceTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="35sp"
        android:textColor="#fff"
        android:layout_marginRight="5dp"
        />
</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值