【Android 开发教程】自定义ContentProvider的使用

本文介绍了一个基于Android 4的应用案例,展示了如何通过ContentProvider进行数据的添加、查询、更新及删除操作。文中提供了具体的XML布局代码及Java实现细节,帮助读者理解如何在Android应用中利用ContentProvider管理书籍数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


现在,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);

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值