2013-05-13 32 views
0

我正在使用Web服務在Android中構建應用程序。我想要做的是同步功能。我從Web服務器獲取一些屬性,並且我有一個布爾值,如果布爾值爲true,那麼它將更新我下載的文檔。如何在Android中更新sqlite數據庫和手機存儲中的文檔?

但我被困在這裏要麼我需要先刪除文件,然後將其替換,否則我可以直接替換文件。代碼是這樣的:

SoapObject DocResponse = (SoapObject)envelope.getResponse();  
     Log.i("Uspdated Documentss", DocResponse.toString()); 
     for(int i=0; i < DocResponse.getPropertyCount(); i++) 
     { 
      SoapObject SingleSubFolder = (SoapObject)DocResponse.getProperty(i);   
      ID = SingleSubFolder.getProperty(0).toString(); 
      fileLongName = SingleSubFolder.getProperty(1).toString(); 
      UserFileName = SingleSubFolder.getProperty(2).toString(); 
      url = SingleSubFolder.getProperty(3).toString(); 
      fileExtension = SingleSubFolder.getProperty(4).toString(); 
      lastModifiedDate = SingleSubFolder.getProperty(5).toString(); 
      SubjectType = SingleSubFolder.getProperty(6).toString(); 
      IsUpdated = SingleSubFolder.hasProperty("IsUpdated"); 
      db = new DMS_Database(context); 
      if(IsUpdated==true) 
      { 

// How to perform replace function from database here??? 
// Is there any need of download manager so that again it download new folder into the phone storage or into the database??? 

      DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 
      request.setTitle(UserFileName); 
      // in order for this if to run, you must use the android 3.2 to compile your app 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
       { 
        request.allowScanningByMediaScanner(); 
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
       } 
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS + "/Downloads", UserFileName); 
        DownloadManager manager = (DownloadManager)Activtyclass.mAct.getSystemService(Context.DOWNLOAD_SERVICE); 
        manager.enqueue(request); 
      } 
      db.close(); 
     } 
} 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
     Toast.makeText(context, " Network Exception : " + e 
       + "Please check network connectivity.", Toast.LENGTH_LONG).show(); 
    } 

數據庫:這是我的數據庫方法...是不是正確?

public void update_Doc(String Id, String name, String url) 
{ 
SQLiteDatabase db = this.getWritableDatabase(); 
ContentValues updatenote = new ContentValues(); 
updatenote.put(DOCUMENT_ID, Id); 
updatenote.put(DOCUMENT_NAME, name); 
updatenote.put(DOCUMENT_URL, url); 
db.update(DOCUMENT_TABLE, updatenote, DOCUMENT_ID + "=?" , new String[] {String.valueOf(Id)}); 
} 

回答

0
public void update_Doc(String Id, String name, String url) 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues updatenote = new ContentValues(); 
    updatenote.put(DOCUMENT_ID, Id); 
    updatenote.put(DOCUMENT_NAME, name); 
    updatenote.put(DOCUMENT_URL, url); 
    long id = db.insertWithOnConflict(DOCUMENT_TABLE, null, updatenote, SQLiteDatabase.CONFLICT_IGNORE); 
    if (id == -1) 
    { 
     db.update(DOCUMENT_TABLE, updatenote, DOCUMENT_ID + "=?" , new String[] {String.valueOf(Id)}); 
    } 

} 
+0

但如何實現這種活性的方法? – Shweta 2013-05-13 08:59:36

+0

你在你的數據庫類中實現它,只需在你的活動中調用update_Doc – 2013-05-13 09:00:52

+0

好吧,是否有任何需要在活動中使用下載管理器?db = new DMS_Database(context); \t \t \t \t如果(IsUpdated == TRUE) \t \t \t \t { \t \t \t \t \t db.update_Doc(ID,UserFileName,URL); – Shweta 2013-05-13 09:01:43

相關問題