2016-03-08 71 views
0

我是一個相當新的開發人員。 我正在構建一個AndroidStudio應用程序,該應用程序使用PullParser從服務器讀取XML文件。我需要從xml鏈接下載一個file.zip,當我按下buttonDownload(在listView的原始佈局內)時,它應該開始下載文件。 以下是我的SitesAdapter的行佈局代碼。從XMLPullParser下載文件

public View getView(final int pos, final View convertView, final ViewGroup parent){ 
    RelativeLayout row = (RelativeLayout)convertView; 
    Log.i("AtlantisSites", "getView pos = " + pos); 
    //ViewHolder mainViewHolder = null; 
    ViewHolder viewHolder; 
    if(null == row){ 
//No recycled View, we have to inflate one. 
     LayoutInflater inflater =(LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = (RelativeLayout)inflater.inflate(R.layout.row_site, null); 

     viewHolder = new ViewHolder(); 
     viewHolder.btnDownload = (Button) row.findViewById(R.id.btnDownload); 
     viewHolder.btnDownload.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Integer pos=(Integer)v.getTag(); 
       Log.i("AtlantisSites", "getView pos = " + pos); 
       String url = getItem(pos).getLink(); 
       Intent i = new Intent(Intent.ACTION_VIEW); 
       i.setData(Uri.parse(url)); 
      } 
     }); 
row.setTag(viewHolder); 
    } else{ 
     viewHolder = (ViewHolder) row.getTag(); 
    } 
viewHolder.btnDetails.setTag(getItem(pos)); 

public class ViewHolder{ 
    Button btnDownload; 
    Button btnDetails; 
} 

這裏是類,我應該叫getLink()開始下載file.zip

public class DownloadZip { 



public void DownloadFromUrlZip(){ 
    try { 


     URL url = new URL (s); 

     HttpURLConnection c = (HttpURLConnection)url.openConnection(); 
     c.setRequestMethod("GET"); 
     c.setDoOutput(true); 
     c.connect(); 

     String Path = Environment.getExternalStorageDirectory() + "/download/"; 
     Log.v("AtlantisSites", "PATH: " + Path); 
     File file = new File(Path); 
     file.mkdirs(); 
     FileOutputStream fos = new FileOutputStream("AtlantisIssue.zip"); 

     InputStream is = c.getInputStream(); 

     byte[] buffer = new byte[1024]; 
     int len1 = 0; 
     while ((len1 = is.read(buffer)) != -1) { 
      fos.write(buffer, 0, len1); 
     } 
     fos.close(); 
     is.close(); 
    } catch (IOException e) { 
     Log.d("AtlantisSites", "Error: " + e); 
    } 
    Log.v("AtlantisSites", "Check: "); 

} 

下載我要解壓縮文件並將其保存在內部存儲後。 關鍵是我不知道如何調用getLink()開始從它下載。 我嘗試了很多次和方法。 我被卡住了。

請,任何幫助將真正apreciate。

回答

0
Intent i = new Intent(v.getContext(), DownloadZip.class); 
v.getContext().startActivity(i); 

我在ViewHolder中的按鈕中寫了這個。並創建一個方法Download.class