2013-08-18 157 views
0

嗨,我已經創建了下載過程活動,並且它在按鈕點擊上運行。此活動在listitem點擊下打開。但是現在我想在lisitem單擊下運行下載過程,單擊按鈕單擊。在MainActivity.java在listitem上需要幫助點擊android

ZipDownloader.java

import java.io.File; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.view.WindowManager; 
import android.widget.Toast; 

import com.kabelash.sg.util.DecompressZip; 
import com.kabelash.sg.util.DownloadFile; 
import com.kabelash.sg.util.ExternalStorage; 
import com.kabelash.sg.R; 

public class ZipDownloader extends Activity { 

    protected ProgressDialog mProgressDialog; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.zipdownload); 

     // Keep the screen (and device) active as long as this app is frontmost. 
     // This is to avoid going to sleep during the download. 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    } 

    /** 
    * Invoked when user presses "Start download" button. 
    */ 
    public void startDownload(View v) { 
     String url = "http://sample.co.uk/sample.zip"; 
     new DownloadTask().execute(url); 
    } 

    /** 
    * Background task to download and unpack .zip file in background. 
    */ 
    private class DownloadTask extends AsyncTask<String,Void,Exception> { 

     @Override 
     protected void onPreExecute() { 
      showProgress(); 
     } 

     @Override 
     protected Exception doInBackground(String... params) { 
      String url = (String) params[0]; 

      try { 
       downloadAllAssets(url); 
      } catch (Exception e) { return e; } 

      return null; 
     } 

    } 

    //Progress window 
    protected void showProgress() { 
     mProgressDialog = new ProgressDialog(this); 
     mProgressDialog.setTitle(R.string.progress_title); 
     mProgressDialog.setMessage(getString(R.string.progress_detail)); 
     mProgressDialog.setIndeterminate(true); 
     mProgressDialog.setCancelable(false); 
     mProgressDialog.show(); 
    } 

    protected void dismissProgress() { 
     // You can't be too careful. 
     if (mProgressDialog != null && mProgressDialog.isShowing() && mProgressDialog.getWindow() != null) { 
      try { 
       mProgressDialog.dismiss(); 
      } catch (IllegalArgumentException ignore) { ; } 
     } 
     mProgressDialog = null; 
    } 



} 

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     super.onOptionsItemSelected(item); 

     switch(item.getItemId()){ 
      case R.id.update: 
       Intent intent = new Intent(this, ZipDownloader.class); 
       startActivity(intent); 
       break; 
     } 
     return true; 

    } 

請不要忽略這個問題。預先感謝併爲我的英語感到遺憾。

+1

「請忽略此問題」。現在我只是困惑。 –

+0

你不想要一個答案? – Pavlos

+0

我不知道他知道他在第二句話中寫了什麼。 – Leandros

回答

1

您是否嘗試過把你的AsyncTask代碼到活動中,您希望您的列表項點擊,那麼就

switch(item.getItemId()){ 
     case R.id.update: 
      String url = "http://sample.co.uk/sample.zip"; 
      new DownloadTask().execute(url); 
      break; 
    } 
    return true; 

調用後臺任務的onclick?

+0

感謝您的想法。希望它能起作用。我試過後會讓你知道。 – Kabil