Insert, Update and Delete Contact Using ContentProvider

Following Code demonstrates insert, update and delete contact using Content provider

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView1"
        android:layout_toLeftOf="@+id/imageView1"
        android:text="Button" />

</RelativeLayout>

ContactIUD.java

package com.example.contactscp5a;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	Cursor c;
	int count=0;
	Uri u,pu;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		createContact("mca5a","1111");
		
		Button b = (Button)findViewById(R.id.button1);
		b.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
                                  createContact("ASDFG");
				//deleteContact("ASDFG");
				//updateContact("pqr");
			}
		});
		
	}
	private void updateContact(String nm)
	{
		ContentValues cv = new ContentValues();
		//cv.put(People.NAME, "mca5a");
		cv.put(People.Phones.TYPE, People.TYPE_HOME);
		cv.put(People.NUMBER, "89898");
	//	Cursor c = managedQuery(People.CONTENT_URI, new String[]{People.NAME}, People.NAME+"=?", new String[]{nm}, null);
		//Toast.makeText(getApplicationContext(), c.getCount()+"", 50).show();
		getContentResolver().update(pu, cv, 
				null,null);
	}
	private void createContact(String nm,String phno)
	{
		ContentValues cv = new ContentValues();
		cv.put(People.NAME, nm);
		u = getContentResolver().insert(People.CONTENT_URI, cv);
		pu = Uri.withAppendedPath(u, People.Phones.CONTENT_DIRECTORY);
		cv.clear();
		cv.put(People.Phones.TYPE, People.TYPE_MOBILE);
		cv.put(People.NUMBER,phno);
		u = getContentResolver().insert(pu, cv);
	}
	private void deleteContact(String nm)
	{
		getContentResolver().delete(People.CONTENT_URI, 
				People.NAME+"=?", new String[]{nm});
		////getContentResolver().delete(u, null, null);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s