zoukankan      html  css  js  c++  java
  • Android 程式开发:(二十)内容提供者 —— 20.6 自定义ContentProvider的使用

    现在,ContentProvider已经创建好了,可以去尝试使用一下。

    1. 使用之前的工程,在布局文件main.xml中添加一些控件。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="ISBN" />
    
    <EditText
        android:id="@+id/txtISBN"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" />
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Title" />
    
    <EditText
        android:id="@+id/txtTitle" 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" />
    
    <Button
        android:text="Add title"
        android:id="@+id/btnAdd"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:onClick="onClickAddTitle" />
    
    <Button
        android:text="Retrieve titles"
        android:id="@+id/btnRetrieve"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onClickRetrieveTitles"  />
    
    </LinearLayout>


    2. 在ContentProvidersActivity.java中,添加测试代码。

    public class ContentProvidersActivity extends Activity {
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    	}
    
    	public void onClickAddTitle(View view) {
    		/*
    		//---add a book---
    		ContentValues values = new ContentValues();
    		values.put(BooksProvider.TITLE, ((EditText)
    				findViewById(R.id.txtTitle)).getText().toString());
    		values.put(BooksProvider.ISBN, ((EditText)
    				findViewById(R.id.txtISBN)).getText().toString());
    		Uri uri = getContentResolver().insert(
    				BooksProvider.CONTENT_URI, values);
    		 */
    
    		ContentValues values = new ContentValues();
    		values.put("title", ((EditText)
    				findViewById(R.id.txtTitle)).getText().toString());
    		values.put("isbn", ((EditText)
    				findViewById(R.id.txtISBN)).getText().toString());
    		Uri uri = getContentResolver().insert(
    				Uri.parse(
    						"content://net.manoel.provider.Books/books"),
    						values);
    
    
    		Toast.makeText(getBaseContext(),uri.toString(),
    				Toast.LENGTH_LONG).show();
    	}
    
    	public void onClickRetrieveTitles(View view) {
    		//---retrieve the titles---
    		Uri allTitles = Uri.parse(
    				"content://net.manoel.provider.Books/books");
    		
    		Cursor c; 
    		if (android.os.Build.VERSION.SDK_INT <11) {
    			//---before Honeycomb---
    			c = managedQuery(allTitles, null, null, null,
    					"title desc");
    		} else {
    			//---Honeycomb and later---
    			CursorLoader cursorLoader = new CursorLoader(
    					this, 
    					allTitles, null, null, null,
    					"title desc");
    			c = cursorLoader.loadInBackground();        	
    		}
    		
    		if (c.moveToFirst()) {
    			do{
    				Toast.makeText(this, 
    						c.getString(c.getColumnIndex(
    								BooksProvider._ID)) + ", " +
    								c.getString(c.getColumnIndex(
    										BooksProvider.TITLE)) + ", " +
    										c.getString(c.getColumnIndex(
    												BooksProvider.ISBN)),
    												Toast.LENGTH_SHORT).show();
    			} while (c.moveToNext());
    		}
    	}
    
    	public void updateTitle() {
    		ContentValues editedValues = new ContentValues();
    		editedValues.put(BooksProvider.TITLE, "Android Tips and Tricks");
    		getContentResolver().update(
    				Uri.parse(
    						"content://net.manoel.provider.Books/books/2"),
    						editedValues,
    						null,
    						null);
    	}
    
    	public void deleteTitle() {
    
    		//---delete a title---
    		getContentResolver().delete(
    				Uri.parse("content://net.manoel.provider.Books/books/2"),
    				null, null);
    
    
    		//---delete all titles---
    		getContentResolver().delete(
    				Uri.parse("content://net.manoel.provider.Books/books"),
    				null, null);
    
    	}
    
    }


  • 相关阅读:
    0107. Binary Tree Level Order Traversal II (E)
    0052. N-Queens II (H)
    0051. N-Queens (H)
    0441. Arranging Coins (E)
    面向对象的三大特性
    Java面向对象
    Java方法
    Java流程控制
    Scanner 类
    Java基础语法
  • 原文地址:https://www.cnblogs.com/james1207/p/3297482.html
Copyright © 2011-2022 走看看