2014-12-05 41 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仅供参考。