Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android如何更新listview中搜索项目的进度栏_Android_Listview - Fatal编程技术网

android如何更新listview中搜索项目的进度栏

android如何更新listview中搜索项目的进度栏,android,listview,Android,Listview,在我的应用程序中,我有一个包含多个项目的列表视图,每个项目都有自己的进度条。当我点击项目时,下载开始,但进度条仅显示在第一个项目上,当我再次启动第二个项目下载时,进度条显示在第一个项目上!有什么想法吗 以下是downloader类的代码: public class ApkUpdater { private Activity ApkUpdater; private String applicationApk; private DownloadManager manager;

在我的应用程序中,我有一个包含多个项目的列表视图,每个项目都有自己的进度条。当我点击项目时,下载开始,但进度条仅显示在第一个项目上,当我再次启动第二个项目下载时,进度条显示在第一个项目上!有什么想法吗

以下是downloader类的代码:

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;
    }
}
公共类UpdateListViewAdapter扩展了BaseAdapter{
//声明变量
语境;
充气机;
private ArrayList applicationList=null;
私有ArrayList ArrayList;
私有字符串类别;
私有ApkUpdater更新程序;
ProgressBar updateProgress;
公共UpdateListViewAdapter(上下文上下文,ArrayList应用程序列表)
{
mContext=上下文;
this.updater=新的ApkUpdater((活动)mContext);
this.applicationList=应用程序列表;
充气器=从(mContext)开始的充气器;
this.arraylist=新的arraylist();
this.arraylist.addAll(applicationList);
}
公共类视图持有者
{
ImageView徽标;
TextView appName;
文本视图开发人员;
文本浏览率;
//ProgressBar ProgressBar;
}
@凌驾
public int getCount(){
返回applicationList.size();
}
@凌驾
公共应用程序POJO getItem(int位置){
返回applicationList.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
公共视图getView(最终整数位置、视图视图、视图组父视图)
{
最终持票人;
如果(视图==null)
{
holder=新的ViewHolder();
视图=充气机。充气(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);
视图.设置标签(支架);
}
其他的
{
holder=(ViewHolder)view.getTag();
}
试一试{
字符串logonName=applicationList.get(位置).getLogo();
位图logoImg=BitmapFactory.decodeStream((InputStream)新URL(IPClass.SERVERIP+LogonName).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());
}
捕获(例外情况除外)
{
Log.d(“适配器异常”,例如toString());
}
view.setOnClickListener(新的view.OnClickListener()
{@覆盖
公共void onClick(视图arg0)
{
字符串路径=applicationList.get(位置).getPath();
String fileName=applicationList.get(position.getFileName();
int size=applicationList.get(position.getSize();
setParameters(路径、文件名、大小);
}
});
返回视图;
}
}

您应该创建一个模型类,并在
UpdateListViewAdapter
中使用它的
列表。
模型类可以包含具有当前进度值的整数字段。
您可以在
public View getView(final int position,View View,ViewGroup parent)
方法中将其值设置为进度条。
当模型类的值更改时,您可以从
活动
类调用
UpdateListViewAdapter
实例上的
notifyDataSetChanged()
来刷新视图。

您应该创建一个模型类,并在
UpdateListViewAdapter
中使用其
列表。
模型类可以包含具有当前进度值的整数字段。
您可以在
public View getView(final int position,View View,ViewGroup parent)
方法中将其值设置为进度条。 当模型类的值更改时,您可以从
活动
类调用
UpdateListViewAdapter
实例上的
notifyDataSetChanged()
,以刷新视图