gpt4 book ai didi

android - 下载文件后更改 ListView 项目按钮

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:19 27 4
gpt4 key购买 nike

我在这个项目中创建了一个项目,我下载了运行良好的 PDF 文件表单服务器。

这是我的界面

enter image description here

但我想要的是在下载文件后更改按钮的图标。并使用此按钮打开下载的 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<User> {
Context context;
int layoutResourceId;
ArrayList<User> data = new ArrayList<User>();
static View row;
static DownloadTask downloadTask;

public UserCustomAdapter(Context context, int layoutResourceId,
ArrayList<User> 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<String, Integer, String> {

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

}

最佳答案

您可以修改您的getView() 方法。尝试执行以下步骤。1. 首先,从您的本地文件夹中获取所有下载的 PDF。2. 将获取的列表传递给您的适配器。3. 当您在 get view 方法中膨胀并创建 View 时,首先,尝试将当前名称与下载的文件名相匹配。4. 如果当前文件已经下载,则将下载按钮更改为打开按钮。而且,如果之前没有下载过,则只显示下载按钮。有了这个,当您重新打开应用程序时,您将获得所需的输出。

对于下载完成时的情况,只需尝试执行 notifySetDataChanged。它将根据上述模式自动重新创建 ListView 。希望你明白。:)

关于android - 下载文件后更改 ListView 项目按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37497193/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com