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;
	   static{
	      uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
	      uriMatcher.addURI(PROVIDER_NAME, "customers", CUSTOMERS);
	      uriMatcher.addURI(PROVIDER_NAME, "customers/#", CUSTOMERS_ID);
	   }
	@Override
	public boolean onCreate() {
        
        }

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

	@Override
	public Uri insert(Uri uri, ContentValues values) {

	}

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

	}
	
	@Override
	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");
	      
	      values.put(BirthProvider.CITY,"Haifa");

	      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()) {
                do{  
                      result = result + c.getString(c.getColumnIndex(CustProvider.NAME)) 
	    	 }while(c.moveToNext());
	      }
	     
}

 

 

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

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

Compare Products

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

{{compareToolModel.Error}}

Now comparing:

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