如何设置数组以更改多个按钮的异步下载任务的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");

}


异步任务本身:

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);
}

最佳答案

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很难


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

10-06 07:22