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:
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
- package com.stallion.codingonandroid;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.app.Activity;
- import android.content.ContentValues;
- import android.database.Cursor;
- public class UsingMyProvider extends Activity {
- private Uri uri = Uri.parse("content://com.stallion.contentprovider");
- private TextView textView;
- private Button b1,b2;
- private EditText e1,e2,e3;
- String s1,s2,s3;
- Cursor cursor;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_using_my_provider);
- textView = (TextView)findViewById(R.id.textView1);
- b1 = (Button)findViewById(R.id.button1);
- b2 = (Button)findViewById(R.id.button2);
- e1 = (EditText)findViewById(R.id.editText1);
- e2 = (EditText)findViewById(R.id.editText2);
- e3 = (EditText)findViewById(R.id.editText3);
- b1.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- s1 = e1.getText().toString();
- s2 = e2.getText().toString();
- s3 = e3.getText().toString();
- ContentValues values = new ContentValues();
- values.put("id", s1);
- values.put("name", s2);
- values.put("coarse", s3);
- getContentResolver().insert(uri, values);
- }
- });
- b2.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- cursor = managedQuery(uri, null, null, null, null);
- if(cursor != null || cursor.getCount() != 0){
- textView.setText("");
- cursor.moveToFirst();
- do{
- textView.append("\nId : " +cursor.getString(0));
- textView.append("\n Name : " +cursor.getString(1));
- textView.append("\n Course : " +cursor.getString(2));
- }while(cursor.moveToNext());
- }
- }
- });
- }
- }
This comment has been removed by the author.
ReplyDelete