2017-04-20 52 views
0

這裏是我的代碼:改造asynchrone塔什

private void InitialContact(final Context context) 
{ 
    final ContactDatabase db = new ContactDatabase(context); 
    context.openOrCreateDatabase("****", MODE_PRIVATE, null); 
    if(refrech){ 
     refrech(db, context); 
    } 
    getcontact(db, context); 
    db.close(); 
} 

一個getContact fonction(在主線程)

private void getcontact(final ContactDatabase db, final Context context){ 
    SQLiteDatabase dbread = db.getReadableDatabase(); 
    String[] retour = { 
     "pseudo", 
     "adresse", 
     "image", "ID" 
     }; 
    Cursor cursor = dbread.query("Contact", retour, null, null, null, null, "pseudo"); 
    while(cursor.moveToNext()) { 
     String pseudo = cursor.getString(cursor.getColumnIndexOrThrow("pseudo")); 
     Log.d("debug", "getcontact: "+pseudo); 
     listcontact.add(new Contact(pseudo)); 
    } 
    cursor.close(); 
} 

而且Asynchrone任務刷新

private void refrech(final ContactDatabase db, final Context context){ 
    final SQLiteDatabase dbwrite = db.getWritableDatabase(); 
    db.onRefresh(dbwrite); 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); 
    int ID = preferences.getInt("ID", 0); 
    if(ID != 0){ 
     ApiInterface mApiService = this.getInterfaceService(); 
     Call<ContactCall> mService = mApiService.getcontact(ID); 
     mService.enqueue(new Callback<ContactCall>() { 
      @Override 
      public void onResponse(Call<ContactCall> call, Response<ContactCall> response) { 
       ContactCall ContactCallobjet = response.body(); 
       ArrayList pseudo = ContactCallobjet.isLogin; 
       for(int i = 0 ;pseudo.size() <= i; i++){ 
        ContentValues values = new ContentValues(); 
        String contact = (String) pseudo.get(i); 
        values.put("pseudo", contact); 
        values.put("adresse", "test"); 
        values.put("image", "test"); 
        values.put("ID", "5"); 
        dbwrite.insert("Contact", null, values); 
       } 
      } 

      @Override 
      public void onFailure(Call<ContactCall> call, Throwable t) { 
       Log.d("DEBUG", "populateWithInitialContacts: "+t.getMessage()); 
      } 
     }); 
    } else { 
     Log.d("DEBUG", "populateWithInitialContacts: Error ID"); 
    } 
} 

我probleme是getContact函數在異步函數之前調用(refrech) 我測試了一個回調函數,但它沒有工作

回答

0

有你getContact方法在你onResponse和失敗回調被稱爲:

private void InitialContact(final Context context) { 
    final ContactDatabase db = new ContactDatabase(context); 
    context.openOrCreateDatabase("****", MODE_PRIVATE, null); 
    if(refrech){ 
     refrech(db, context); 
    } else { 
     getcontact(db, context); 
    } 
    db.close(); 
} 

private void refrech(final ContactDatabase db, final Context context){ 
    final SQLiteDatabase dbwrite = db.getWritableDatabase(); 
    db.onRefresh(dbwrite); 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); 
    int ID = preferences.getInt("ID", 0); 
    if(ID != 0){ 
     ApiInterface mApiService = this.getInterfaceService(); 
     Call<ContactCall> mService = mApiService.getcontact(ID); 
     mService.enqueue(new Callback<ContactCall>() { 
      @Override 
      public void onResponse(Call<ContactCall> call, Response<ContactCall> response) { 
       ContactCall ContactCallobjet = response.body(); 
       ArrayList pseudo = ContactCallobjet.isLogin; 
       for(int i = 0 ;pseudo.size() <= i; i++){ 
        ContentValues values = new ContentValues(); 
        String contact = (String) pseudo.get(i); 
        values.put("pseudo", contact); 
        values.put("adresse", "test"); 
        values.put("image", "test"); 
        values.put("ID", "5"); 
        dbwrite.insert("Contact", null, values); 
        // call get contacts after the response 
        getcontact(db, context); 
       } 
      } 

      @Override 
      public void onFailure(Call<ContactCall> call, Throwable t) { 
       Log.d("DEBUG", "populateWithInitialContacts: "+t.getMessage()); 
       // call get contacts on error 
       getcontact(db, context); 
      } 
     }); 
    } else { 
     Log.d("DEBUG", "populateWithInitialContacts: Error ID"); 
     // call get contacts on error id 
     getcontact(db, context); 
    } 
} 
+0

這似乎是一個問題要問(還包括錯誤日誌堆棧跟蹤和關於聯繫平板電腦的信息)。如果你發現這個答案解決了你的問題,那麼接受它作爲答案 – petey

+0

事實上,這不是一個可行的答案,因爲異步任務。 我的'私人列表 listcontact;' 我填寫了'listcontact.add(new Contact(pseudo));' 在tash結束時清楚 – therapha361