Friday, 22 March 2013

How to create my own cotent provider in android

A content provider manages access to a central repository of data.A provider is a part of an Adnroid application,which often provides its own UI for working with the data.However,content providers are primarily intended to be used by other applications, which access the provider using a provider client object.Together,providers and provider clients offer a consistent,standard interfaceto data that also handles inter-process communication and securedata access.

This code describes the basics of the following:

  • How content providers work.
  • The API you use retrieve data from a content provider.
  • The API you use to insert,update,or delete data in a contentprovider.
  • other API features that facilitate working wiht providers.
  • The following code is useful to create content provider 
    1. package com.stallion.codingonandroid;

  1. import android.net.Uri;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.view.View.OnClickListener;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8. import android.app.Activity;
  9. import android.content.ContentValues;
  10. import android.database.Cursor;

  11. public class UsingMyProvider extends Activity {

  12. private Uri uri = Uri.parse("content://com.stallion.contentprovider");
  13. private TextView textView;
  14. private Button b1,b2;
  15. private EditText e1,e2,e3;
  16.     String s1,s2,s3;
  17.     Cursor cursor;
  18. @Override
  19.     public void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_using_my_provider);
  22.         textView = (TextView)findViewById(R.id.textView1);
  23.         b1 = (Button)findViewById(R.id.button1);
  24.         b2 = (Button)findViewById(R.id.button2);
  25.         e1 = (EditText)findViewById(R.id.editText1);
  26.         e2 = (EditText)findViewById(R.id.editText2);
  27.         e3 = (EditText)findViewById(R.id.editText3);
  28.         
  29.         
  30.         b1.setOnClickListener(new OnClickListener() {
  31. public void onClick(View v) {
  32.        s1 = e1.getText().toString();
  33.        s2 = e2.getText().toString();
  34.        s3 = e3.getText().toString();
  35.        ContentValues values = new ContentValues();
  36.        values.put("id", s1);
  37.        values.put("name", s2);
  38.        values.put("coarse", s3);
  39.        getContentResolver().insert(uri, values);
  40. }
  41. });
  42.         
  43.         b2.setOnClickListener(new OnClickListener() {
  44. public void onClick(View v) {
  45. cursor = managedQuery(uri, null, null, null, null);
  46. if(cursor != null || cursor.getCount() != 0){
  47. textView.setText("");
  48. cursor.moveToFirst();
  49. do{
  50. textView.append("\nId : " +cursor.getString(0));
  51. textView.append("\n Name : " +cursor.getString(1));
  52. textView.append("\n Course : " +cursor.getString(2));
  53. }while(cursor.moveToNext());
  54. }
  55. }
  56. });
  57.     }

  58. }

                                                                                                                                  1 comment: