The ContentProvider class of the shared parameter is bound to the database. How can I use the shared parameter test class to test the addition, deletion, modification, and query functions of the database?

Source: Internet
Author: User

Intent can upload an object

When two interfaces are redirected, an object needs to be passed in the past by using the Bundle class, and the object class needs to be serializable for serialization. The transfer method is as follows:

Defines a static constant as the key value.


Public final static String SER_KEY = "com. xiaoshu. worker ";

Intent intent = new Intent ();

Intent. setClass (WorkerActivity. this, DisplayWorker. class );

Bundle bundle = new Bundle ();

Bundle. putSerializable (SER_KEY, worker );

Intent. putExtras (bundle );

StartActivity (intent );

In another interface, you can receive the objects passed through intent. The receiving method is as follows:

Worker worker = (Worker) getIntent (). getSerializableExtra (WorkerActivity. SER_KEY );

 


/**

* 1. Create a class MyProvide, inherit from ContentProvider, and implement four methods, which need to be registered. Called in activity.

* Registration is as follows:

<Provider android: name = ". MyProvide" android: authorities = "www.android1.com.cn"> </provider>

*

* 2. Use a listener button to obtain the MyProvide parser in another MainActivity.

* ContentResolver resolver = MainActivity. this. getContentResolver (); // obtain the shared data when listening, one query and one insert

* Uri uri = Uri. parse ("content: // www.android1.com.cn"); // uri indicates the resource address.

* Resolver. query (uri, null); query

* ContentValues values = new ContentValues (); // insert an object, because the insert method needs to pass a values value.

* Resolver. insert (uri, values );

*

 

 

 

* ****************** Execute the MainActivity of the database **************** ********

Public class MainActivity extends Activity {


@ Override

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. activity_main );

Button button = (Button) findViewById (R. id. sss_sss );

Button. setOnClickListener (new OnClickListener (){


@ Override

Public void onClick (View v ){

// Obtain the resolver object for content parsing and perform operations on the database through this object

ContentResolver resolver = MainActivity. this. getContentResolver ();

Uri uri = Uri. parse ("content: // www.android1.com.cn ");

Resolver. query (uri, null, null );

ContentValues values = new ContentValues ();

Values. put ("name", "zhaoshan ");

Resolver. insert (uri, values );

 


}

});

 

}


@ 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;

}


}

**************************************** ***************************************

/* You need to register <provider android: name = ". MyProvide" android: authorities = "www.android1.com.cn"> </provider>

* The content provider is bound to the database. Other programs cannot ask the database of the program, but can access the database through the content creator.

*/


Public class MyProvide extends ContentProvider {

SQLiteDatabase db = null;


@ Override

Public boolean onCreate (){

// Instantiate the database

MyDBHelper helper = new MyDBHelper (this. getContext ());

Db = helper. getWritableDatabase ();

Return true;

}


@ Override

Public Cursor query (Uri uri, String [] projection, String selection,

String [] selectionArgs, String sortOrder ){

// Query the database and return a cursor value.

Return db. query ("Users", projection, selection, selectionArgs, null,

Null, sortOrder );


}


@ Override

Public Uri insert (Uri uri, ContentValues values ){

Db. insert ("Users", "_ id", values );

Return uri;

}


@ Override

Public int delete (Uri uri, String selection, String [] selectionArgs ){

Return db. delete ("Users", selection, selectionArgs );

}


@ Override

Public int update (Uri uri, ContentValues values, String selection,

String [] selectionArgs ){

Int s = db. update ("Users", values, selection, selectionArgs );

Return s;

}


@ Override

Public String getType (Uri uri ){

Return null;

}


}

// ************** Create a database szt ********************** ****

Public class MyDBHelper extends SQLiteOpenHelper {


Public MyDBHelper (Context context ){

Super (context, "szt. db", null, 1 );

}


@ Override

Public void onCreate (SQLiteDatabase db ){

// Create a data table Users

Db.exe cSQL ("create table Users (_ id integer primary key autoincrement, name text )");

}

@ Override

Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){

// TODO Auto-generated method stub


}


}

 

**************************************** ***************************************

/** Test data of other applications

* First, we know the sharing parameters of other programs. This is the registration format that inherits the ContentProvider sharing parameter class:

* <Provider android: name = ". MyProvide" android: authorities = "www.android1.com.cn"> </provider>

* 1. TestMyProvide test class first adds the permission to the resource file in the Instrumentation --> add ----> name ---> android. test. InstrumentationTestRunner

* ----> Target package ---> com. example. testprovider package name

* 2. register the test resource file

* <Application

Android: allowBackup = "true"

Android: icon = "@ drawable/ic_launcher"

Android: label = "@ string/app_name"

Android: theme = "@ style/AppTheme">

<Activity

Android: name = "com. example. testprovider. MainActivity"

Android: label = "@ string/app_name">

<Intent-filter>

<Action android: name = "android. intent. action. MAIN"/>


<Category android: name = "android. intent. category. LAUNCHER"/>

</Intent-filter>

</Activity>

<Uses-library android: name = "android. test. runner"/> // Add this fixed format

</Application>

*

* 3. After registration, this class inherits the AndroidTestCase class and overwrites the test database method. The method name starts with "test.

* In outline mode, the following five methods of the TestMyProvide class are displayed: setUp ();

* TestInsert ();

* TestQuery ();

* Testdelete ();

* TestUpdate ();

* Select a method and right-click the running method ---> Run As ---> Android jUnit Test.

* In the MyProvide class, view the database after data -- date -- package name -- export the database

*/

Public class TestMyProvide extends AndroidTestCase {

ContentResolver resolver = null;

@ Override

Protected void setUp () throws Exception {

Resolver = this. getContext (). getContentResolver ();

Super. setUp ();

}


Public void testInsert (){

ContentValues values = new ContentValues ();

Values. put ("name", "wangchenghua ");

Resolver. insert (Uri. parse ("content: // www.android1.com.cn"), values );

Log. I ("msg", "wangchenghua ");

}

Public void testQuery (){

Cursor c = resolver. query (Uri. parse ("content: // www.android1.com.cn"), null );

While (c. moveToNext ()){

Log. I ("msg", c. getString (c. getColumnIndex ("name ")));

}

C. close ();

}

Public void testdelete (){

Resolver. delete (Uri. parse ("content: // www.android1.com.cn"), "_ id =? ", New String [] {String. valueOf (1 )});

}

Public void testUpdate (){

ContentValues values = new ContentValues ();

Values. put ("name", "wangxiaoou ");

Resolver. update (Uri. parse ("content: // www.android1.com.cn"), values, "_ id =? ", New String [] {String. valueOf (4 )});

}

 

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.