2014-12-05 55 views
1

我將一條記錄插入到一​​個DB中,該DB在兩個字段(Name和DOB)上具有唯一的構造函數。我從Async任務的DoInBackground()方法插入。我這樣做如下:正確的方法來終止異步任務並通知用戶:

try{ 
         insertInDb(name.trim(),start_date.trim(),imagePath.trim(), String.valueOf(TYPE),String.valueOf(SELECTION), name_friend.trim(), isAdhoc); 
        }catch (Exception e){ 
         System.out.println("Caught up here"+e); 
         return "Record Exists"; 
        } 

這裏是我的postExecute():

public void onPostExecute(String result){ 
      //TODO 
      if(result.equalsIgnoreCase("Record Exists")){ 
       if(Asycdialog !=null && Asycdialog.isShowing()) 
        Asycdialog.dismiss(); 
       Toast.makeText(getApplicationContext(), "Record Exists please try a different name and year", Toast.LENGTH_LONG).show(); 

      }else if(result.equalsIgnoreCase("Success")){ 
       if(Asycdialog !=null && Asycdialog.isShowing()){ 
        Asycdialog.dismiss(); 

        Intent mainIntent; 
        mainIntent = new Intent(AdhocCase.this,MainActivity.class); 
        AdhocCase.this.startActivity(mainIntent); 
        AdhocCase.this.finish(); 
       } 
      } 

中的情況下,敬酒消息「記錄存在」從doInBackground返回的時候似乎永遠不會執行。但意圖確實發生了變化。

這裏是我的完整doInBackground():

protected String doInBackground(Void... params) { 
    // TODO Auto-generated method stub 

    try{ 
     System.out.println("Type in async: "+type); 
     System.out.println("Selection in async: "+String.valueOf(SELECTION)); 
     System.out.println("TYPE in async: "+String.valueOf(TYPE)); 
     dbHelper.open(); 

     Cursor checkCur = dbHelper.checkIfRecordExists(name.trim(), start_date.trim()); 
     System.out.println("Cursor count"+checkCur.getCount()); 
     if(checkCur != null && checkCur.moveToFirst()){ 
      System.out.println("Exists"); 
      return "Record Exists"; 
     }else{ 
      System.out.println("Image path: "+imagePath); 
      insertAdhocCase(toTitleCase(name).trim(),start_date.trim(),imagePath, type, String.valueOf(SELECTION), toTitleCase(name_friend).trim()); 

      try{ 
       insertInDb(name.trim(),start_date.trim(),imagePath.trim(), String.valueOf(TYPE),String.valueOf(SELECTION), name_friend.trim(), isAdhoc); 
      }catch (Exception e){ 
       System.out.println("Caught up here"+e); 
       return "Record Exists"; 
      } 
     // LC.writeToSDCard(); 
     } 

    }catch(Exception e){ 
     // System.out.println(e); 
    } 

    return "Success"; 
} 

}

+0

你是不是指'return「記錄存在」;'不工作? – 2014-12-05 06:01:50

+0

你不能在'doInBackground()'中吐司'信息' – Gattsu 2014-12-05 06:03:27

+0

我在onPostExecute matey中敬酒。 – User3 2014-12-05 06:04:22

回答

0

我認爲這個問題是您

Toast.makeText(getApplicationContext(), "Record Exists please try a different name and year", Toast.LENGTH_LONG).show(); 

必須明確提供從調用活動類似上下文

Toast.makeText(context.getApplicationContext(), "Record Exists please try a different name and year", Toast.LENGTH_LONG).show(); 

Check this僅供參考。

相關問題