我需要取消我的asynctask填充列表视图适配器之前,它已经完成按后退按钮。通过这个代码
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);convertView=inflater.inflate(R.layout.seatadapter, null);
TextView text3=(TextView)convertView.findViewById(R.id.text3);
ProgressBar progressBar1=(ProgressBar)convertView.findViewById(R.id.progressBar1);
TextView predic=(TextView)convertView.findViewById(R.id.textView3);
TextView classs=(TextView)convertView.findViewById(R.id.textView4);
ImageView img1=(ImageView)convertView.findViewById(R.id.imageView1);
new update(classs,predic, text3, progressBar1,img1).execute(tarinname.get(position), Date.get(position), Ffrom.get(position), Tto.get(position), Ccl.get(position));
class update extends AsyncTask{
@Override
protected Object doInBackground(Object... params) {
Code For Json Parsing....
}
protected void onPostExecute(Object result) {
super.onPostExecute(result);
if (this.result.contains("CurrentStatus")) {
progressBar.setVisibility(progressBar.GONE);
t.setVisibility(t.VISIBLE);
t3.setVisibility(t3.VISIBLE);
im1.setVisibility(im1.VISIBLE);
t3.setText("Class "+TravelClass);
t1.setText(perdcition);
if (ConfirmTktStatus.contains("Confirm")) {
im1.setImageResource(R.drawable.img_green);
}
if (ConfirmTktStatus.contains("Probable")) {
}
if (ConfirmTktStatus.contains("No Chance")) {
}
t.setText(var1);
}else {
progressBar.setVisibility(progressBar.GONE);
}
Toast.makeText(context, var, Toast.LENGTH_LONG).show();
}
这是一个适配器类,我们无法通过按后退键来取消它。按下后退键,活动被关闭,但后台任务仍在继续。
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
((Activity)context).finish();
}
我已经试过了,但是没有用…!!
请创建一个列表来存储当前的AsyncTask,然后随时取消它
for (AsyncTask myAsyncTask : myAsyncTasks) {
if (myAsyncTask.getStatus().equals(AsyncTask.Status.RUNNING)) {
myAsyncTask.cancel(true);
}
}
myAsyncTasks.clear();
详细代码:
private List<AsyncTask> myAsyncTasks = new ArrayList<>();
public void addRunningTask(AsyncTask task) {
myAsyncTasks.add(task);
}
public void cancelRunningTasks() {
for (AsyncTask myAsyncTask : myAsyncTasks) {
if (myAsyncTask.getStatus().equals(AsyncTask.Status.RUNNING)) {
myAsyncTask.cancel(true);
}
}
myAsyncTasks.clear();
}
@Override
public void onBackPressed() {
cancelRunningTasks();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);convertView=inflater.inflate(R.layout.seatadapter, null);
TextView text3=(TextView)convertView.findViewById(R.id.text3);
ProgressBar progressBar1=(ProgressBar)convertView.findViewById(R.id.progressBar1);
TextView predic=(TextView)convertView.findViewById(R.id.textView3);
TextView classs=(TextView)convertView.findViewById(R.id.textView4);
ImageView img1=(ImageView)convertView.findViewById(R.id.imageView1);
AsyncTask task = new update(classs,predic, text3, progressBar1,img1);
task.execute(tarinname.get(position), Date.get(position), Ffrom.get(position), Tto.get(position), Ccl.get(position));
addRunningTask(task);
class update extends AsyncTask{
@Override
protected Object doInBackground(Object... params) {
Code For Json Parsing....
}
protected void onPostExecute(Object result) {
super.onPostExecute(result);
if (this.result.contains("CurrentStatus")) {
progressBar.setVisibility(progressBar.GONE);
t.setVisibility(t.VISIBLE);
t3.setVisibility(t3.VISIBLE);
im1.setVisibility(im1.VISIBLE);
t3.setText("Class "+TravelClass);
t1.setText(perdcition);
if (ConfirmTktStatus.contains("Confirm")) {
im1.setImageResource(R.drawable.img_green);
}
if (ConfirmTktStatus.contains("Probable")) {
}
if (ConfirmTktStatus.contains("No Chance")) {
}
t.setText(var1);
}else {
progressBar.setVisibility(progressBar.GONE);
}
Toast.makeText(context, var, Toast.LENGTH_LONG).show();
}