android listview下载应用,android-下载文件后更改了listview项目按钮

我在这个项目中创建了一个项目,我下载了PDF文件格式服务器,效果很好.

这是我的UI

dc5856e01388c429fc2eca0f8c855405.png

但是我要的是下载文件后更改按钮的图标.然后使用此按钮打开下载的PDf文件.

请帮我

UserCustomAdapter.java

import java.io.File;

import java.util.ArrayList;

import android.app.Activity;

import android.content.Context;

import android.os.Environment;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;

public class UserCustomAdapter extends ArrayAdapter {

Context context;

int layoutResourceId;

ArrayList data = new ArrayList();

static View row;

static DownloadTask downloadTask;

public UserCustomAdapter(Context context, int layoutResourceId,

ArrayList data) {

super(context, layoutResourceId, data);

this.layoutResourceId = layoutResourceId;

this.context = context;

this.data = data;

}

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

row = convertView;

UserHolder holder = null;

if (row == null) {

LayoutInflater inflater = ((Activity) context).getLayoutInflater();

row = inflater.inflate(layoutResourceId, parent, false);

holder = new UserHolder();

holder.tv_paper_name = (TextView) row.findViewById(R.id.tv_paper_name);

// holder.tv_paper_desc = (TextView) row.findViewById(R.id.tv_paper_desc);

holder.bt_download = (Button) row.findViewById(R.id.bt_download);

row.setTag(holder);

} else {

holder = (UserHolder) row.getTag();

}

User user = data.get(position);

holder.tv_paper_name.setText(user.getName());

// holder.tv_paper_desc.setText(user.getAddress());

// holder.textLocation.setText(user.getLocation());

final UserHolder finalHolder = holder;

holder.bt_download.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Log.i("Download Button Clicked", "**********");

// Toast.makeText(context, "Download "+ finalHolder.tv_paper_name.getText().toString()+" " + position,

// Toast.LENGTH_LONG).show();

File extStore = Environment.getExternalStorageDirectory();

File myFile = new File(extStore.getAbsolutePath() + "/Exam Papers/"+finalHolder.tv_paper_name.getText().toString()+".pdf");

if (!myFile.exists()) {

// execute this when the downloader must be fired

downloadTask = new DownloadTask(context);

/* downloadTask.execute("http://ia.tranetech.ae:82/upload/uploads/five-point-someone-chetan-bhagat_ebook.pdf",""+finalHolder.tv_paper_name.getText().toString()+".pdf");*/

downloadTask.execute("https://letuscsolutions.files.wordpress.com/2015/07/five-point-someone-chetan-bhagat_ebook.pdf",""+finalHolder.tv_paper_name.getText().toString()+".pdf");

} else {

Toast.makeText(context, "File already Exists in "+myFile, Toast.LENGTH_SHORT).show();

}

}

});

return row;

}

static class UserHolder {

TextView tv_paper_name;

// TextView tv_paper_desc;

Button bt_download;

}

}

下载Task.java

public class DownloadTask extends AsyncTask {

Context context;

private PowerManager.WakeLock mWakeLock;

ProgressDialog mProgressDialog;

private static final int MEGABYTE = 1024 * 1024;

DownloadTask downloadTask;

String Name;

public DownloadTask(Context context) {

this.context = context;

}

@Override

protected void onPreExecute() {

super.onPreExecute();

// take CPU lock to prevent CPU from going off if the user

// presses the power button during download

// instantiate it within the onCreate method

mProgressDialog = new ProgressDialog(context);

mProgressDialog.setMessage("Downloading....");

mProgressDialog.setIndeterminate(true);

mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

mProgressDialog.setCancelable(true);

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,

getClass().getName());

mWakeLock.acquire();

mProgressDialog.show();

mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

@Override

public void onCancel(DialogInterface dialog) {

String sdcard_path = Environment.getExternalStorageDirectory().getPath();

File file = new File(sdcard_path + "/Exam Papers/"+Name+".pdf");

file.delete();

Toast.makeText(context, "Download In Background", Toast.LENGTH_SHORT).show();

}

});

}

@Override

protected String doInBackground(String... str) {

String URL = str[0];

Name = str[1];

InputStream input = null;

OutputStream output = null;

HttpURLConnection connection = null;

try {

URL url = new URL(URL);

connection = (HttpURLConnection) url.openConnection();

connection.connect();

// expect HTTP 200 OK, so we don't mistakenly save error report

// instead of the file

if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {

return "Server returned HTTP " + connection.getResponseCode()

+ " " + connection.getResponseMessage();

}

// this will be useful to display download percentage

// might be -1: server did not report the length

int fileLength = connection.getContentLength();

// download the file

input = connection.getInputStream();

String sdcard_path = Environment.getExternalStorageDirectory().getPath();

Log.d("Path ------ ", " " + sdcard_path);

// create a File object for the parent directory

File PapersDiractory = new File(sdcard_path + "/Exam Papers/");

// have the object build the directory structure, if needed.

PapersDiractory.mkdirs();

// create a File object for the output file

File outputFile = new File(PapersDiractory, ""+Name);

// now attach the OutputStream to the file object, instead of a String representation

output = new FileOutputStream(outputFile);

// output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/five-point-someone-chetan-bhagat_ebook.pdf");

byte data[] = new byte[MEGABYTE];

long total = 0;

int count;

while ((count = input.read(data)) != -1) {

// allow canceling with back button

if (isCancelled()) {

input.close();

return null;

}

total += count;

// publishing the progress....

if (fileLength > 0) // only if total length is known

publishProgress((int) (total * 100 / fileLength));

int progress= (int) (total * 100 / fileLength);

Log.d("Progress = ", "" + (int) (total * 100 / fileLength));

output.write(data, 0, count);

}

} catch (Exception e) {

return e.toString();

} finally {

try {

if (output != null)

output.close();

if (input != null)

input.close();

} catch (IOException ignored) {

}

if (connection != null)

connection.disconnect();

}

return null;

}

@Override

protected void onProgressUpdate(Integer... progress) {

super.onProgressUpdate(progress);

// if we get here, length is known, now set indeterminate to false

mProgressDialog.setIndeterminate(false);

mProgressDialog.setMax(100);

mProgressDialog.setProgress(progress[0]);

}

@Override

protected void onPostExecute(String result) {

mWakeLock.release();

mProgressDialog.dismiss();

if (result != null)

Toast.makeText(context, "Download error: " + result, Toast.LENGTH_LONG).show();

else

Toast.makeText(context, "File downloaded", Toast.LENGTH_SHORT).show();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值