0

我使用自定义 Baseadapter 类显示了列表项。我的问题是,当我单击位置 1 的 listview 按钮时,也单击了位置 other 的按钮。但是我下载了完整的视频。但是 listview 显示不正确。我该如何克服这个问题?

这是我的代码:

public class TestHopeListNew extends Activity {
    ListView lv;
    ArrayList<Url_Dto> list = new ArrayList<Url_Dto>();
    MyListAdapter adtf;
    public static String prf_date_view = "";
    String str_start;
    Button all_detail;
    Button accepted_all, mainDownloadBtn;
    public static SQLiteDatabase db;
    String name;
    File download;
    int i = 0;
    private ArrayList<ProgressBarSeek> progreeSeekList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_hope_list_new);
        progreeSeekList = new ArrayList<ProgressBarSeek>();
        list = DBAdapter.getUrl_Detail();
        Log.v("log_tag", "list  :: " + list.size());
        lv = (ListView) findViewById(R.id.main_list_meet);
        mainDownloadBtn = (Button) findViewById(R.id.not_shown);
        adtf = new MyListAdapter(this);
        lv.setAdapter(adtf);
        adtf.notifyDataSetChanged();

        SqliteAdpter dba = SqliteAdpter
                .getAdapterInstance(getApplicationContext());
        dba.createdatabase();
        db = dba.openDataBase();

        mainDownloadBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                adtf.setAllDownload();
            }
        });

    }

    public class MyListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        ProgressBar pr;
        ProgressBar[] prArray = new ProgressBar[list.size()];
        Button cl, dl;
        ImageView im;
        DownloadFileFromURL downloadFileFromURL;
        ViewHolder holder = null;

        public MyListAdapter(Context context) {
            mInflater = LayoutInflater.from(context);

        }

        public int getCount() {
            return list.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public void setAllDownload() {
            if (prArray.length > 0) {
                for (int i = 0; i < prArray.length; i++) {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    Log.v("log_tag", "list.get(i).url_video   "
                            + list.get(i).url_video);
                    downloadFileFromURL = new DownloadFileFromURL(dl, cl);
                    downloadFileFromURL.execute(pr, list.get(i).url_video, i);
                }
            }
        }

        private class ViewHolder {
            public Button cl;
            public Button dl;
            public ProgressBar pr;
            public ImageView im;

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

            /*convertView = mInflater.inflate(R.layout.custome_list_view, null);
            cl = (Button) convertView.findViewById(R.id.cancle_sedual);
            dl = (Button) convertView.findViewById(R.id.download_sedual);
            pr = (ProgressBar) convertView.findViewById(R.id.listprogressbar);
            prArray[position] = pr;
            im = (ImageView) convertView.findViewById(R.id.list_image);
            im.setImageResource(list.get(position).images[position]);*/

            //final ViewHolder holder = null;



            if (convertView == null) {
                LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = mInflater.inflate(R.layout.custome_list_view,
                        null);
                holder = new ViewHolder();
                prArray[position] = pr;
                holder.cl = (Button) convertView
                        .findViewById(R.id.cancle_sedual);
                holder.dl = (Button) convertView
                        .findViewById(R.id.download_sedual);
                holder.pr = (ProgressBar) convertView
                        .findViewById(R.id.listprogressbar);
                holder.im = (ImageView) convertView
                        .findViewById(R.id.list_image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.im.setImageResource(list.get(position).images[position]);
            // pr.setProgress(getItem(position));

            holder.cl.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Log.v("log_tag", "Cancle Button Click");


                    holder.dl.setVisibility(View.VISIBLE);
                    holder.cl.setVisibility(View.GONE);
                    //downloadFileFromURL = new DownloadFileFromURL(dl, cl);
                     downloadFileFromURL.cancel(true);
                    downloadFileFromURL.downloadFile();
                    holder.pr.setProgress(0);
                    notifyDataSetChanged();
                }
            });

            holder.dl.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    holder.dl.setFocusable(false);
                    str_start = list.get(position).url_video;
                    holder.dl.setVisibility(View.GONE);
                    holder.cl.setVisibility(View.VISIBLE);
                    Log.v("log_tag", "Start Button Click ");

                    // new DownloadFileFromURL().execute(str_start);
                    downloadFileFromURL = new DownloadFileFromURL(holder.dl, holder.cl);
                    downloadFileFromURL.execute(holder.pr, str_start, position);
                    notifyDataSetChanged();
                }
            });

            getProgress(holder.pr, position, holder.dl, holder.cl);
        //  convertView.setTag(holder);
            return convertView;
        }

    }

    class DownloadFileFromURL extends AsyncTask<Object, String, Integer> {

        int count = 0;
        ProgressDialog dialog;
        ProgressBar progressBar;
        int myProgress;
        int position;
        Button start, cancel;
        boolean download1 = false;

        public DownloadFileFromURL(Button start, Button cancel) {
            this.start = start;
            this.cancel = cancel;
        }

        /**
         * Before starting background thread Show Progress Bar Dialog
         * */

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            ProgressBar progressBar;
            download1 = true;

        }

        public void downloadFile() {
            this.download1 = false;

        }

        @Override
        protected void onCancelled() {
            super.onCancelled();

        }

        /**
         * Downloading file in background thread
         * */
        @Override
        protected Integer doInBackground(Object... params) {

            int count;
            progressBar = (ProgressBar) params[0];
            position = (Integer) params[2];
            try {

                URL url = new URL((String) params[1]);

                name = ((String) params[1]).substring(((String) params[1])
                        .lastIndexOf("/") + 1);
                // Log.v("log_tag", "name Substring ::: " + name);
                URLConnection conection = url.openConnection();
                conection.setConnectTimeout(2000);
                conection.connect();
                // getting file length
                int lenghtOfFile = conection.getContentLength();

                // input stream to read file - with 8k buffer
                InputStream input = new BufferedInputStream(url.openStream(),
                        8192);
                download = new File(Environment.getExternalStorageDirectory()
                        + "/download/");
                if (!download.exists()) {
                    download.mkdir();
                }
                String strDownloaDuRL = download + "/" + name;
                Log.v("log_tag", " down url   " + strDownloaDuRL);
                FileOutputStream output = new FileOutputStream(strDownloaDuRL);

                byte data[] = new byte[1024];

                long total = 0;

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

                        if(isCancelled()){
                            break;
                        }
                        total += count;

                        // writing data to file
                        progressBar
                                .setProgress((int) ((total * 100) / lenghtOfFile));
                        output.write(data, 0, count);
                        setProgress(progressBar, position, start, cancel, this);

                    } else {
                        File delete = new File(strDownloaDuRL);
                        delete.delete();
                    }
                }
                // flushing output
                output.flush();

                // closing streams
                output.close();
                input.close();

            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }
            return 0;

        }

        /**
         * Updating progress bar
         * */
        protected void onProgressUpdate(String... values) {

            super.onProgressUpdate(values);
            // Log.v("log_tag", "progress :: " + values);
            // setting progress percentage
            // pDialog.setProgress(Integer.parseInt(progress[0]));
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {

            Log.v("log", "login  ::: 4::: " + download);
            String videoPath = download + "/" + name;
            String chpName = name;
            Log.v("log_tag", "chpName  ::::" + chpName + "  videoPath "
                    + videoPath);
            db.execSQL("insert into videoStatus (chapterNo,videoPath) values(\""
                    + chpName + "\",\"" + videoPath + "\" )");

        }

    }

    private void setProgress(final ProgressBar pr, final int position,
            final Button Start, final Button cancel,
            final DownloadFileFromURL downloadFileFromURL) {
        ProgressBarSeek pbarSeek = new ProgressBarSeek();
        pbarSeek.setPosition(position);
        pbarSeek.setProgressValue(pr.getProgress());
        // Log.v("log_tag", position + "  progress  " + pr.getProgress());
        progreeSeekList.add(pbarSeek);
        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.v("log_tag", "Cancle Button Click Set progress");
                Start.setVisibility(View.VISIBLE);
                cancel.setVisibility(View.GONE);
                downloadFileFromURL.cancel(true);
                downloadFileFromURL.downloadFile();
                pr.setProgress(0);
            }
        });
        Start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.v("log_tag", "Start Button Click set Progress");
                str_start = list.get(position).url_video;
                Start.setVisibility(View.GONE);
                cancel.setVisibility(View.VISIBLE);
                Log.v("log_tag", "str_start  " + str_start);
                //
                // new DownloadFileFromURL().execute(str_start);
                DownloadFileFromURL downloadFileFromU = new DownloadFileFromURL(
                        Start, cancel);
                downloadFileFromU.execute(pr, str_start, position);
            }
        });
    }

    private void getProgress(ProgressBar pr, int position, Button dl, Button cl) {
        if (progreeSeekList.size() > 0) {
            for (int j = 0; j < progreeSeekList.size(); j++) {
                if (position == progreeSeekList.get(j).getPosition()) {
                    pr.setProgress(progreeSeekList.get(j).getProgressValue());
                    dl.setVisibility(View.GONE);
                    cl.setVisibility(View.VISIBLE);
                }
            }
        }
    }
}

在此处输入图像描述

我单击第二个按钮但显示其他按钮 clickeble 但第二个按钮背景下载在 sd 卡中但显示不正确。如何解决它以及我的代码有什么问题,请帮助我!!!

4

0 回答 0