Register your Content Provider in your activity file using provider tag Here is

Register your content provider in your activity file

This preview shows page 3 - 5 out of 10 pages.

Register your Content Provider in your activity file using <provider> tag. Here is the list of methods which you need to override in Content Provider class to have your Content Provider working: onCreate() This method is called when the provider is started. query() This method receives a request from a client. The result is returned as a Cursor object. insert() This method inserts a new record into the content provider. delete() This method deletes an existing record from the content provider. update() This method updates an existing record from the content provider. getType() This method returns the MIME type of the data at the given URI. Exercise This lab assignment shows how to create your own ContentProvider . The steps to complete this lab assignment are as follows: 1. You will use Android Studio to create an Android application and name it as MyContentProvider under a package com.example.mycontentprovider, with blank Activity. 2. Modify main activity file MainActivity.java to add two new methods onClickAddName() and onClickRetrieveStudents(). 3. Create a new java file called StudentsProvider.java under the package com.example.mycontentprovider to define your actual provider and associated methods. 4. Register your content provider in your AndroidManifest.xml file using <provider.../> tag 5. Modify the default content of res/layout/activity_main.xml file to include a small GUI to add students records. 6. Define required constants in res/values/strings.xml file 7. Run the application to launch Android emulator and verify the result of the changes done in the application.
Image of page 3
In the MainActivity.java file add two new methods onClickAddName() and onClickRetrieveStudents() to handle user interaction with the application. package com.example.mycontentprovider; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentValues; import android.content.CursorLoader; import android.database.Cursor; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } public void onClickAddName(View view) { // Add a new student record ContentValues values = new ContentValues(); values.put(StudentsProvider.NAME, ((EditText)findViewById(R.id.txtName)).getText().toString()); values.put(StudentsProvider.GRADE, ((EditText)findViewById(R.id.txtGrade)).getText().toString()); Uri uri = getContentResolver().insert( StudentsProvider.CONTENT_URI, values); Toast.makeText(getBaseContext(), uri.toString(), Toast.LENGTH_LONG).show(); } public void onClickRetrieveStudents(View view) { // Retrieve student records String URL = "content://com.example.provider.College/students"; Uri students = Uri.parse(URL); Cursor c = managedQuery(students, null, null, null, "name"); if (c.moveToFirst()) { do{ Toast.makeText(this, c.getString(c.getColumnIndex(StudentsProvider._ID)) +
Image of page 4
Image of page 5

You've reached the end of your free preview.

Want to read all 10 pages?

  • Fall '12
  • JoshuaO'Connor
  • static final String, content provider

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture