Udacity – czego nauczyłam się z kursu cz.7

  1. ContentProvider – implementacja update

@Override public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) 
{ 
//Keep track of if an update occurs 
int tasksUpdated; 
// match code 
int match = sUriMatcher.match(uri); 

switch (match) 
{ 
  case TASK_WITH_ID: 
       //update a single task by getting the id 
        String id = uri.getPathSegments().get(1); 
      //using selections 
         tasksUpdated = mTaskDbHelper.getWritableDatabase().update(TABLE_NAME, values, "_id=?", new String[]{id}); 
         break; 
  default: 
     throw new UnsupportedOperationException("Unknown uri: " + uri); } 
  
 if (tasksUpdated != 0) 
    { //set notifications if a task was updated 
        getContext().getContentResolver().notifyChange(uri, null); } 
     // return number of tasks updated 
    return tasksUpdated; 
    }

2) ContentProvider – Implementacja delete

@Override public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {       int numRowsDeleted;     
     if (null == selection) selection = "1";     

        switch (sUriMatcher.match(uri)) 
         {         case CODE_WEATHER:                        numRowsDeleted = mOpenHelper.getWritableDatabase().delete(WeatherContract.WeatherEntry.TABLE_NAME, selection, selectionArgs);                         break;         
 default:  throw new UnsupportedOperationException("Unknown uri: " + uri);     
         }       

   if (numRowsDeleted != 0) 
        {         
             getContext().getContentResolver().notifyChange(uri, null);        }     return numRowsDeleted; }

3) ContentProvider – implementacja query

@Override
    
 public Cursor query(@NonNull Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder)
 {
 
     
    // Get access to underlying database (read-only for query)
        
 final SQLiteDatabase db = mTaskDbHelper.getReadableDatabase();
    

    // Write URI match code
  
   // Write a query for the tasks directory and default case
 
  

      int match = sUriMatcher.match(uri);
     

   Cursor retCursor;
 


     switch (match) {
    
        // Query for the tasks directory
    

        case TASKS:
   
              retCursor =  db.query(TABLE_NAME,
projection,
selection,
selectionArgs,null,null,
sortOrder);
 
               break;


             // Add a case to query for a single row of data by ID
            
 // Use selections and selectionArgs to filter for that ID
 
       case TASK_WITH_ID:
              
  // Get the id from the URI
  
               String id = uri.getPathSegments().get(1);
 

                // Selection is the _ID column = ?, and the Selection args = the row ID from the URI
                String mSelection = "_id=?";
  
              String[] mSelectionArgs = new String[]{id};
 
  
              // Construct a query as you would normally, passing in the selection/args
               
 retCursor =  db.query(TABLE_NAME,
projection,
mSelection,
mSelectionArgs,null,null,
sortOrder);
                break;
 
        
    // Default exception
 
           default:
                throw new UnsupportedOperationException("Unknown uri: " + uri);
        }
 
      

 // Set a notification URI on the Cursor
 
       retCursor.setNotificationUri(getContext().getContentResolver(), uri);
 
 
       // Return the desired Cursor
   
     return retCursor;
    }

4) ContentProvider -implementacja getType

/* getType() handles requests for the MIME type of data
We are working with two types of data:
1) a directory and 2) a single row of data.
This method will not be used in our app, but gives a way to standardize the data formats
that your provider accesses, and this can be useful for data organization.
For now, this method will not be used but will be provided for completeness.
 */ @Override public String getType(@NonNull Uri uri) 
{      int match = sUriMatcher.match(uri);    
   switch (match) {     
    case TASKS:        
     // directory      
       return "vnd.android.cursor.dir" + "/" + TaskContract.AUTHORITY + "/" + TaskContract.PATH_TASKS;       
  case TASK_WITH_ID:      
       // single item type    
         return "vnd.android.cursor.item" + "/" + TaskContract.AUTHORITY + "/" + TaskContract.PATH_TASKS;     
    default:       
      throw new UnsupportedOperationException("Unknown uri: " + uri);     } }

 try {  for (ContentValues value:values)  {       long weatherDate=value.getAsLong(WeatherContract.WeatherEntry.COLUMN_DATE);       if (!SunshineDateUtils.isDateNormalized(weatherDate))       {          throw new IllegalArgumentException("Date must be normalized to insert");      }       long _id=db.insert(WeatherContract.WeatherEntry.TABLE_NAME,null,value);       if (_id!=-1)       {          rowsInserted++;      }  }      db.setTransactionSuccessful(); } finally {     db.endTransaction(); }

 

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *