2016-05-28 11 views
0

プロジェクトでは、私は動作時に実行時にデータを更新する必要があります。しかし、ここで私は更新しようとしています。Androidコンテンツプロバイダがデータを更新できません

private void saveData() 
    { 
     String name1 = name.getText().toString(); 
     String phone = mobile.getText().toString(); 
     String address = city.getText().toString(); 
     if (TextUtils.isEmpty(name1)||TextUtils.isEmpty(phone)||TextUtils.isEmpty(address)) 
     { 
      if (TextUtils.isEmpty(name1)) 
      { 
       name.setError("Feild can not be Empty"); 
      } 
      else if (TextUtils.isEmpty(phone)) 
      { 
       mobile.setError("Feild can not be Empty"); 
      } 
      else if (TextUtils.isEmpty(address)) 
      { 
       city.setError("Feild can not be Empty"); 
      } 
     } 
     else 
     { 
      ContentValues values = new ContentValues(); 
      values.put(DataProvider.Pname,name1); 
      values.put(DataProvider.Pphone,phone); 
      values.put(DataProvider.Paddress,address); 
      values.put(DataProvider.PCategory,"Debt"); 
      values.put(DataProvider.Pamount,totamount);      
      int a = getContentResolver().update(DataProvider.ContentUri_Person,values,DataProvider.PID+"=?",new String[]{String.valueOf(logid)}); 
      Toast.makeText(AddLog.this, String.valueOf(a)+"Value updated", Toast.LENGTH_SHORT).show(); 
      startActivity(returnback); 
      finish(); 
     } 
    } 

コンテンツプロバイダクラス

public class DataProvider extends ContentProvider 
{ 
    static final String ProviderName = "com.example.mrudu.accounts.provider"; 
    static final String URLPerson = "content://"+ProviderName+"/Person_Detail"; 
    static final String URLDetails = "content://"+ProviderName+"/Details"; 
    static final Uri ContentUri_Person = Uri.parse(URLPerson); 
    static final Uri ContentUri_Details = Uri.parse(URLDetails); 

    //Tables 
    private static String PTableName = "Person_Detail"; 
    private static String DTableName = "Details"; 

    public static long insertId = 0; 

    //Person_Detail Coloumns 
    public static String PID = "_id"; 
    public static String Pname = "PName"; 
    public static String Pphone = "PMobile"; 
    public static String Paddress = "PCity"; 
    public static String PCategory = "PCategory"; 
    public static String Pamount = "PAmount"; 

    //Details coloumn 
    public static String DID = "_id"; 
    public static String Dname = "DName"; 
    public static String Damount = "DAmount"; 
    public static String Ddate = "DDate"; 
    public static String DCategory = "DCategory"; 
    public static String Per_In = "Per_In"; 

    private static final int Person = 1; 
    private static final int Person_ID = 2; 
    private static final int Details = 3; 
    private static final int Details_Id = 4; 

    static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 
    static 
    { 
     uriMatcher.addURI(ProviderName,PTableName,Person); 
     uriMatcher.addURI(ProviderName,PTableName+"/#",Person_ID); 
     uriMatcher.addURI(ProviderName,DTableName,Details); 
     uriMatcher.addURI(ProviderName,DTableName+"/#",Details_Id); 
    } 

    public static SQLiteDatabase sqLiteDatabase; 
    private static String Databasename = "Accounts"; 
    private static int DatabaseVersion = 1; 

    private class DatabaseHelper extends SQLiteOpenHelper 
    { 

     public DatabaseHelper(Context context) 
     { 
      super(context, Databasename, null, DatabaseVersion); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase sqLiteDatabase) 
     { 
      String Create_Person = " Create Table "+PTableName+"("+PID+" INTEGER PRIMARYKEY ,"+Pname+" TEXT ,"+Pphone+" TEXT ,"+Paddress+" TEXT ,"+PCategory+" TEXT ,"+Pamount+" REAL"+")"; 
      String Create_Details = " Create Table "+DTableName+"("+DID+" INTEGER PRIMARYKEY ,"+Dname+" TEXT ,"+DCategory+" TEXT ,"+Damount+" REAl ,"+Ddate+" TEXT ,"+Per_In+" INTEGER)"; 
      sqLiteDatabase.execSQL(Create_Person); 
      sqLiteDatabase.execSQL(Create_Details); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) 
     { 
      sqLiteDatabase.execSQL("Drop TABLE if exists"+PTableName); 
      sqLiteDatabase.execSQL("Drop TABLE if exists"+DTableName); 
      onCreate(sqLiteDatabase); 
     } 
    } 
    @Override 
    public boolean onCreate() 
    { 
     Context context = getContext(); 
     DatabaseHelper databaseHelper = new DatabaseHelper(context); 
     sqLiteDatabase = databaseHelper.getWritableDatabase(); 
     return (sqLiteDatabase==null)?false:true; 
    } 
    @Override 
    public Uri insert(Uri uri, ContentValues values) 
    { 
     switch (uriMatcher.match(uri)) 
     { 
      case Person: 
       long rowId = sqLiteDatabase.insert(PTableName,null,values); 
       insertId = rowId; 
       if (rowId>0) 
       { 
        Uri _uri = ContentUris.withAppendedId(ContentUri_Person,rowId); 
        getContext().getContentResolver().notifyChange(_uri,null); 
        return _uri; 
       } 
       break; 
      case Details: 
       long rowId1 = sqLiteDatabase.insert(DTableName,null,values); 
       if (rowId1>0) 
       { 
        Uri _uri = ContentUris.withAppendedId(ContentUri_Details,rowId1); 
        getContext().getContentResolver().notifyChange(_uri,null); 
        return _uri; 
       } 
       break; 
     } 
     return null; 
    } 
    @Override 
    public Cursor query(Uri uri, String[] projection, String selection, 
         String[] selectionArgs, String sortOrder) 
    { 
     SQLiteQueryBuilder sqLiteQueryBuilder = new SQLiteQueryBuilder(); 

     switch (uriMatcher.match(uri)) 
     { 
      case Person_ID: 
       sqLiteQueryBuilder.setTables(PTableName); 
       sqLiteQueryBuilder.appendWhere(PID+ "="+ uri.getPathSegments().get(1)); 
       break; 
      case Person: 
       sqLiteQueryBuilder.setTables(PTableName); 
       break; 
      case Details_Id: 
       sqLiteQueryBuilder.setTables(DTableName); 
       sqLiteQueryBuilder.appendWhere(Per_In +"="+ uri.getPathSegments().get(1)); 
       break; 
      case Details: 
       sqLiteQueryBuilder.setTables(DTableName); 
       break; 
      default: 
       throw new UnsupportedOperationException("Not yet implemented"); 
     } 
     Cursor cursor = sqLiteQueryBuilder.query(sqLiteDatabase,projection,selection,selectionArgs,null,null,sortOrder); 
     cursor.setNotificationUri(getContext().getContentResolver(),uri); 
     return cursor; 

    } 
    @Override 
    public int update(Uri uri, ContentValues values, String selection, 
         String[] selectionArgs) 
    { 
     int count = 0; 
     switch (uriMatcher.match(uri)) 
     { 
      case Person: 
       count = sqLiteDatabase.update(PTableName,values,selection,selectionArgs); 
       Log.d("Update","Person"); 
       break; 
      case Person_ID: 
       count = sqLiteDatabase.update(PTableName,values,PID+" = "+uri.getPathSegments().get(1)+(!TextUtils.isEmpty(selection)?" AND (" + selection + ')':""),selectionArgs); 
       Log.d("Update","Person_Id"); 
       break; 
      case Details: 
       count = sqLiteDatabase.update(DTableName,values,selection,selectionArgs); 
       Log.d("Update","Details"); 
       break; 
      case Details_Id: 
       count = sqLiteDatabase.update(DTableName,values,Per_In+" = "+uri.getPathSegments().get(1)+(!TextUtils.isEmpty(selection)?" AND (" + selection + ')':""),selectionArgs); 
       Log.d("Update","Details_Id"); 
       break; 
      default: 
       throw new IllegalArgumentException("Unknown URI " + uri); 
     } 
     getContext().getContentResolver().notifyChange(uri,null); 
     return count; 
    } 
    @Override 
    public int delete(Uri uri, String selection, String[] selectionArgs) { 
     // Implement this to handle requests to delete one or more rows. 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 

    @Override 
    public String getType(Uri uri) { 
     // TODO: Implement this to handle requests for the MIME type of the data 
     // at the given URI. 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 
} 

:データはここ

は私のコードで挿入/更新/削除操作のためのContentProviderを使用していますdatabase.Iに更新されていませんありがとうございました

答えて

0

確認できる問題は2つあります。まず、あなたはCRUD操作の開始時にSQLiteDatabaseを初期化することができます

SQLiteDatabase sqlDB = dbHelper.getWritableDatabase();

秒1 - SQLiteQueryBuilderを使用して、クエリ文字列を確認してください。そこにSQLクエリで問題があるかもしれません。

+0

動作しませんでした... – Satyajeet

関連する問題