600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Android 程式开发:(二十)内容提供者 —— 20.6 自定义ContentProvider的使用

Android 程式开发:(二十)内容提供者 —— 20.6 自定义ContentProvider的使用

时间:2019-12-01 00:25:49

相关推荐

Android 程式开发:(二十)内容提供者 —— 20.6 自定义ContentProvider的使用

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

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

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/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" /><EditTextandroid:id="@+id/txtISBN"android:layout_height="wrap_content"android:layout_width="fill_parent" /><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Title" /><EditTextandroid:id="@+id/txtTitle" android:layout_height="wrap_content"android:layout_width="fill_parent" /><Buttonandroid:text="Add title"android:id="@+id/btnAdd"android:layout_width="fill_parent" android:layout_height="wrap_content"android:onClick="onClickAddTitle" /><Buttonandroid: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. */@Overridepublic 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);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。