Android - Eclipse:“令牌上的语法错误”(“,此标记后面的表达式”

时间:2012-05-04 09:40:10

标签: java android arrays button download

如何设置数组以更改多个按钮的异步下载任务的URL? 这不起作用,我不知道如何解决它。

final String[] dlUrl = new String[]{"www.google.com/document.pdf", "www.google.com/document2.pdf", "www.google.com/document3.pdf"};{ 
for(int k = 0; i < dlUrl.length; i++) { 
    final String dlUrl = downloadFile.execute([k]); 


}

我想让它更改以下代码部分:

private void startDownload() {
    DownloadFile downloadFile = new DownloadFile();
    downloadFile.execute("http://www.google.com/doc.pdf");

}

Async Task本身:

class DownloadFile extends AsyncTask<String, Integer, String> {

URL url = new URL(aurl[0]);
URLConnection connection = url.openConnection();

我有一个类似的数组,可以选择按钮,我想尝试将其添加到它...任何想法?

final OnClickListener listener = new OnClickListener() { 
     public void onClick(View v){ 
         switch(v.getId()){  
         case R.id.sec1:  
            break; 
         case R.id.sec2: 
            break; 
         case R.id.sec3: 
            break; 
         } 
     } 
};
final int[] btnIds = new int[]{R.id.sec1, R.id.sec2, R.id.sec3};{ 
for(int i = 0; i < btnIds.length; i++) { 
    final Button btn = (Button) findViewById (btnIds[i]); 
    btn.setOnClickListener(listener); 
}

1 个答案:

答案 0 :(得分:2)

final String[] dlUrl = new String[]{"www.google.com/document.pdf",
                                    "www.google.com/document2.pdf",
                                    "www.google.com/document3.pdf"};{ 

for(int k = 0; i < dlUrl.length; i++) { 
    final String dlUrl = downloadFile.execute([k]); 


}

应该更像是

final String[] dlUrl = new String[]{"www.google.com/document.pdf",
                                    "www.google.com/document2.pdf",
                                    "www.google.com/document3.pdf"}; // { removed

for(int k = 0; k < dlUrl.length; k++) { 
               ^

    final String currentUrl = dlUrl[k];
                              ^^^^^^^^

    downloadFile.execute(currentUrl); 
}

关于你自己的评论:

  

通过反复试验学习Java和android很困难

......我必须补充一个非常糟糕的主意。