在我的应用程序中,我有一个包含多个项目的列表视图,每个项目都有自己的进度条。当我点击该项目时,下载开始,但进度条仅显示在第一个项目上,当我开始第二个项目时再次下载,进度条被分散在第一个项目上!任何想法?
这是下载程序类的代码:
public class ApkUpdater
{
private Activity ApkUpdater;
private String applicationApk;
private DownloadManager manager;
private String name;
private String cancel;
private String path;
private String fileName;
private long downloadId;
private int size;
private boolean downloading;
private ProgressBar progressBar;
private ImageView cancelBtn;
private ImageView installBtn;
public ApkUpdater(Activity activity){ ApkUpdater = activity; }
public void setParameters(String path, String fileName,int size)
{
this.path = path;
this.fileName = fileName;
this.size = size;
startDownload();
}
public void startDownload()
{
try
{
String url = IPClass.SERVERIP + path;
System.out.println(url);
applicationApk = URLUtil.guessFileName(url + fileName, null, MimeTypeMap.getFileExtensionFromUrl(url + fileName + ".apk"));
DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(url + fileName + ".apk"));
downloadRequest.setTitle("Downloading...");
downloadRequest.setDescription("test");
downloadRequest.allowScanningByMediaScanner();
downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS + "/appsaraai/", applicationApk + ".apk");
manager = (DownloadManager) ApkUpdater.getSystemService(Context.DOWNLOAD_SERVICE);
downloadId = manager.enqueue(downloadRequest);
cancelBtn = (ImageView) ApkUpdater.findViewById(R.id.cancel);
installBtn = (ImageView) ApkUpdater.findViewById(R.id.install);
}
catch(Exception ex)
{
Log.d("Hi", ex.toString());
}
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
progressBar();
int appSizeInBytes = size * 1024 *1024;
int bytes_downloaded;
downloading = true;
cancel = "false";
Cursor cursor;
while (downloading && cancel.equals("false"))
{
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadId);
cursor = manager.query(q);
cursor.moveToFirst();
bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int dl_progress = (int)((bytes_downloaded * 100L)/appSizeInBytes);
if(dl_progress != -1){ progressBar.setProgress(dl_progress); }
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/appsaraai/" + fileName + ".apk")), "application/vnd.android.package-archive");
ApkUpdater.startActivity(intent);
downloading = false;
}
cursor.close();
}
}
});
thread.start();
}
public ProgressBar progressBar()
{
ApkUpdater.runOnUiThread(new Runnable()
{
public void run()
{
progressBar = (ProgressBar) ApkUpdater.findViewById(R.id.updateProgress);
progressBar.setRotation(180);
progressBar.setProgress(0);
progressBar.setMax(100);
}
});
return progressBar;
}
}
这是适配器类的代码:
public class UpdateListViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private ArrayList<ApplicationPojo> applicationList = null;
private ArrayList<ApplicationPojo> arraylist;
private String category;
private ApkUpdater updater;
ProgressBar updateProgress;
public UpdateListViewAdapter(Context context, ArrayList<ApplicationPojo> applicationList)
{
mContext = context;
this.updater = new ApkUpdater((Activity) mContext);
this.applicationList = applicationList;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList();
this.arraylist.addAll(applicationList);
}
public class ViewHolder
{
ImageView logo;
TextView appName;
TextView developer;
TextView rate;
//ProgressBar progressBar;
}
@Override
public int getCount() {
return applicationList.size();
}
@Override
public ApplicationPojo getItem(int position) {
return applicationList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent)
{
final ViewHolder holder;
if (view == null)
{
holder = new ViewHolder();
view = inflater.inflate(R.layout.singleapp, null);
holder.logo = (ImageView) view.findViewById(R.id.appLogo);
holder.appName = (TextView) view.findViewById(R.id.appName);
holder.developer = (TextView) view.findViewById(R.id.developer);
holder.rate = (TextView) view.findViewById(R.id.rateApp);
//updateProgress = holder.progressBar = (ProgressBar) view.findViewById(R.id.updateProgress);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
try {
String logoName = applicationList.get(position).getLogo();
Bitmap logoImg = BitmapFactory.decodeStream((InputStream) new URL(IPClass.SERVERIP + logoName).getContent());
holder.logo.setImageBitmap(logoImg);
holder.appName.setText(applicationList.get(position).getName());
holder.developer.setText(applicationList.get(position).getDeveloper());
holder.rate.setText(String.valueOf(applicationList.get(position).getRate()));
}
catch (Exception ex)
{
Log.d("Adapter Exception", ex.toString());
}
view.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View arg0)
{
String path = applicationList.get(position).getPath();
String fileName = applicationList.get(position).getFileName();
int size = applicationList.get(position).getSize();
updater.setParameters(path,fileName,size);
}
});
return view;
}
}
答案 0 :(得分:0)
您应该创建一个模型类,并在List
中使用其UpdateListViewAdapter
。
您的模型类可以包含具有当前进度值的整数字段。
您可以在public View getView(final int position, View view, ViewGroup parent)
方法中将其值设置为进度条。
当您的模型类的值发生变化时,您可以从notifyDataSetChanged()
类调用UpdateListViewAdapter
实例上的Activity
来刷新视图。