2015-05-29 93 views
-3

我需要取消填充列表視圖適配器之前按下後退按鈕完成列表視圖適配器。通過此代碼在完成之前取消列表視圖適配器的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(); 

      } 

我試過這個已經沒用了.. !!

+1

如何[取消](http://developer.android .com/reference/android/os/AsyncTask.html#cancel(boolean))metod? – Chaosit

+0

更新我試過的問題.. !! –

+0

看看你做什麼:當任務被取消時你完成活動。活動結束後,您應該取消任務。您應該將AsynkTask實例放在某個變量中,並在Activity的onStop生命週期回調中將其取消。 –

回答

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(); 
       } 
+0

我在哪裏把這個代碼.. ??在doInBackGround或其他地方.. ??我正在粘貼它在適配器..! –

+1

首先,您應該將任務插入列表:AsyncTask task = new update(classs,predic,text3,progressBar1,img1)); task.execute ...; myAsyncTasks.add(任務);然後你按後退按鈕 –

+0

初始化數組列表和「更新」時,取消他們是的AsyncTask類名 的ArrayList UPD; 定義數組列表 upd = new ArrayList (); 新的更新(類裏面,predic,文字3,progressBar1,IMG1).execute(tarinname.get(位置),Date.get(位置),Ffrom.get(位置),Tto.get(位置),Ccl.get(位置)); @覆蓋 保護對象doInBackground(對象... PARAMS){ \t \t \t \t爲(的AsyncTask myAsyncTask:UPD)。{ \t \t \t \t \t如果(myAsyncTask.getStatus()等於(AsyncTask.Status.RUNNING )){ \t \t \t \t \t myAsyncTask.cancel(真); \t \t \t \t \t} \t \t \t \t \t} \t \t \t \t \t upd.clear(); –

相關問題