By liran bh | 9/5/2016 | Android Development

Content Provider

Content Provider lets you abstarct a data store so as a client you can add, remove, update and query the data without concern how the data is stored


The Provider code:

public class CustProvider extends ContentProvider {
	 // fields for my content provider
	 static final String PROVIDER_NAME = "com.discoversdk.provider.providename";
	 static final String URL = "content://" + PROVIDER_NAME + "/customers";
	 static final Uri CONTENT_URI = Uri.parse(URL);
	 // fields for the database
	 static final String ID = "id";
	 static final String NAME = "name";
	 static final String CITY = "city";
	 static final int CUSTOMERS = 1;
	 static final int CUSTOMERS_ID = 2;

	 DBHelper dbHelper;
	 static final UriMatcher uriMatcher;
	      uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
	      uriMatcher.addURI(PROVIDER_NAME, "customers", CUSTOMERS);
	      uriMatcher.addURI(PROVIDER_NAME, "customers/#", CUSTOMERS_ID);
	public boolean onCreate() {

	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {

	public Uri insert(Uri uri, ContentValues values) {


	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {

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



Client Code:

public void addCusomer(View view) {
	      // Add a new birthday record
	      ContentValues values = new ContentValues();

	      values.put(CustProvider.NAME, "Eddi");

	      Uri uri = getContentResolver().insert(
	    	CustProvider.CONTENT_URI, values);

 public void showAll(View view) {
	      Cursor c = getContentResolver()
                            .query(CustProvider.CONTENT_URI, null, null, null, "name");
	      if (!c.moveToFirst()) {
                      result = result + c.getString(c.getColumnIndex(CustProvider.NAME)) 



{{CommentsModel.TotalCount}} Comments

Your Comment


Recent Stories

Top DiscoverSDK Experts

User photo
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.


Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now