我需要下载才能启动按钮点击。按钮出现在列表视图的singleitemvew活动中。我看到下载文件的例子分配了一个像这样的网址
private static String file_url = " http:// some example url"
我使用解析来下载文件。如何将值分配给" file_url" 这是我的代码
singleitemvew.java
public class SingleItemView extends Activity
{
ProgressDialog mProgressDialog;
String heading;
String subHeading;
String levelImg;
String apiImg;
String apiText;
String descImgOne;
String descImgTwo;
String codeText;
ImageLoader imgloader = new ImageLoader(this);
String fileurl;
private ProgressDialog pDialog;
ImageView my_image;
// Progress dialog type (0 - for Horizontal progress bar)
public static final int progress_bar_type = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitemview);
Intent i = getIntent();
heading= i.getStringExtra("heading");
subHeading = i.getStringExtra("subheading");
levelImg = i.getStringExtra("levelimg");
apiImg = i.getStringExtra("apiimg");
apiText = i.getStringExtra("apitext");
descImgOne = i.getStringExtra("descimgone");
descImgTwo = i.getStringExtra("descimgtwo");
codeText = i.getStringExtra("codetext");
fileurl = i.getStringExtra("download");
TextView headingtxt = (TextView)findViewById(R.id.singleitemheading);
TextView subheadingtxt = (TextView)findViewById(R.id.singleitemsubheading);
TextView apitxt = (TextView)findViewById(R.id.singleitemviewapitext);
TextView codetxt = (TextView) findViewById(R.id.singleitemviewcodetext);
ImageView level =(ImageView) findViewById(R.id.levelimg);
ImageView api =(ImageView) findViewById(R.id.singleitemviewapiimg);
ImageView descone =(ImageView) findViewById(R.id.descriptionimgone);
ImageView desctwo =(ImageView) findViewById(R.id.descriptionimgtwo);
Button downloadbtn = (Button) findViewById(R.id.singleitemviewButton);
headingtxt.setText(heading);
subheadingtxt.setText(subHeading);
apitxt.setText(apiText);
codetxt.setText(codeText);
imgloader.DisplayImage(levelImg, level);
imgloader.DisplayImage(apiImg, api);
imgloader.DisplayImage(descImgOne, descone);
imgloader.DisplayImage(descImgTwo, desctwo);
downloadbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
new DownloadFileFromURL().execute(file_url);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type: // we set this to 0
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try{
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream
OutputStream output = new FileOutputStream("/sdcard/download/downloadedfile.rar");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
}
}
}
答案 0 :(得分:0)
1.像这样编辑你的DownloadFileFromURL类。
class DownloadFileFromURL extends AsyncTask<String, Void String>
{
String file_url;
public DownloadFileFromURL(String file_url)
{
this.file_url = file_url;
}
//remove URL url = new URL(f_url[0]); from your doInBackground() method
}
2.更改SingleItemViewClass的调用对象
downloadbtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
new DownloadFileFromURL(file_url).execute();
}
});
如果有问题,请告诉我。