我需要在按下后退按钮之前取消填充列表视图适配器的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();
}
我已经尝试过但没有用.. !!
答案 0 :(得分:1)
请创建一个列表来存储当前的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();
}