2016-02-18 103 views
-1

我正在嘗試組合一個簡單的應用程序來幫助我理解內容提供者,但是我無法使其工作。我想插入數據到數據庫中,但我認爲我遇到的問題與URI有關。無法讓Android ContentProvider工作

**我試圖遵循類似的模式如圖所示Udacity教程

這裏是我有什麼 Android清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.android.simplecontentprovider"> 

    <provider 
     android:authorities="com.example.android.simplecontentprovider.app" 
     android:name=".data.MyContentProvider" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

的ContentProvider:

public class MyContentProvider extends ContentProvider { 

    MyDatabaseHelper mOpenHelper; 

    private static final int NAME = 1; 

    // Creates a UriMatcher object. 
    private static final UriMatcher sUriMatcher = buildUriMatcher(); 

    static UriMatcher buildUriMatcher() { 

     final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); 
     final String authority = MyDataContract.CONTENT_AUTHORITY; // Value = com.example.android.simplecontentprovider.app 

     matcher.addURI(authority, MyDataContract.PATH_TABLE, NAME); //PATH_TABLE = tblUserData 
     //matcher.addURI(authority, MyDataContract.PATH_TABLE + "/*", NAME); 

     return matcher; 
    } 

    @Override 
    public boolean onCreate() { 
     mOpenHelper = new MyDatabaseHelper(getContext()); 
     return true; 
    } 

    @Override 
    public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) { 
     return null; 
    } 

    @Override 
    public String getType(Uri uri) { 

     int match = sUriMatcher.match(uri); 
     switch (match){ 
      case NAME: 
       return MyDataContract.UserDetailsEntry.CONTENT_TYPE; 
      default: 
       throw new UnsupportedOperationException("UNKNOWN URI: " + uri); 
     } 

    } 

    @Override 
    public Uri insert(Uri uri, ContentValues contentValues) { 
     final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); 
     final int match = sUriMatcher.match(uri); 
     Uri returnUri; 

     switch (match) { 
      case NAME: { 
       long _id = db.insert(MyDataContract.UserDetailsEntry.TABLE_NAME, null, contentValues); 
       if (_id > 0) 
        returnUri = MyDataContract.UserDetailsEntry.buildtblUserDetailsUri(_id); 
       else 
        throw new android.database.SQLException("Failed to insert row into " + uri); 
       break; 
      } 
      default: 
       throw new UnsupportedOperationException("Unknown uri: " + uri); 
     } 
     getContext().getContentResolver().notifyChange(uri, null); 
     return returnUri; 
    } 

    @Override 
    public int delete(Uri uri, String s, String[] strings) { 
     return 0; 
    } 

    @Override 
    public int update(Uri uri, ContentValues contentValues, String s, String[] strings) { 
     return 0; 
    } 
} 

和最後MainActivity

ContentResolver resolver = getContentResolver(); 

    Uri uri = MyDataContract.UserDetailsEntry.buildtblUserDetailsAll("MyTestName"); 

    Log.e("RESOLVER URI: ", uri.toString()); 

    ContentValues values = new ContentValues(); 
    values.put(MyDataContract.UserDetailsEntry.COLUMN_NAME, "MyTestName"); 

    try { 
     resolver.insert(uri, values); 
    }catch (Exception e){ 
     Log.e("RESOLVER INSERT: ", e.toString()); 
    } 

當我通過調試器中運行它,我得到這個消息:

java.lang.IllegalArgumentException: Unknown URL content://com.example.android.simplecontentprovider.app/tblUserData/MyTestName 

這是在內容提供商插件的默認值。我不知道我是否缺少權威部分

+1

我認爲你應該把你的放在裏面。 – Dave

+0

感謝戴夫,搬遷裏面工作。 – Rohan000

+0

很高興我能幫忙:) – Dave

回答

0
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.android.simplecontentprovider"> 


    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <provider 
      android:authorities="com.example.android.simplecontentprovider.app" 
      android:name=".data.MyContentProvider" /> 
    </application> 

</manifest>