我多次使用过Async Task
但我第一次面对这类问题。我搜索了Stack Overflow但找不到任何有用的解决方案。
我的问题是onPostExecute
未在异步任务中调用,doinbackground
中的所有操作都已完成,但控件未到达onPostExecute
...无法理解原因。
代码:
public class deletedaily extends AsyncTask<Void , Void, long[]>{
ProgressDialog pd;
long resultdelete;
protected void onPreExecute(){
pd=new ProgressDialog(StockDetail.this);
if(pd!=null){
pd.setMessage("Deleting data.....please wait");
pd.show();
}
}
protected long[] doInBackground(Void... params) {
// TODO Auto-generated method stub
try{
Database.getInstance(getApplicationContext()).getWritableDatabase().beginTransaction();
resultdelete = Database.getInstance(getApplicationContext()).getWritableDatabase().delete(st.tablename, st.column2 + "=? AND " + st.column3 + "=?", new String[] {getdailydate.toString(),stockname} );
Database.getInstance(getApplicationContext()).getWritableDatabase().setTransactionSuccessful();
new popdailydata().execute(); //here calling list view to populate after deletion
}
catch(Exception dailydeleteerror){}
finally{
Database.getInstance(getApplicationContext()).getWritableDatabase().endTransaction();
}
return new long[] {resultdelete};
}
protected void onPostExecute(long result){
System.out.println("postexecute entered");
if(pd!=null){
pd.dismiss();
}
if(result!=-1){
Toast.makeText(getApplicationContext(),"Date deleted from your portfolio", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"Failed to delete ....try again", Toast.LENGTH_LONG).show();
}
}
}
我从图像按钮的onclick上打电话
deletedailydata.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new deletedaily().execute();
}
});
答案 0 :(得分:1)
应为onPostExecute(long [] result)
。
您将课程扩展为AsyncTask<Void , Void, long[]>
。这意味着doInBackground的返回值为long[]
,onPostExecute的参数也为。{1}}。你在onPostExecute中获得的是doInBackgroud返回的结果,类型应该是相同的。