1

我在 listview 项目中有按钮,当它被单击时,水平进度条开始加载,当它完成加载时,两个按钮应该出现在 listview 的那个项目中。在下载进度条时,如果我将该项目从屏幕上滚动然后向后滚动,进度条就会消失并出现两个按钮。下载过程正常。但是我想一直在下载时显示进度条?这是我的适配器。请任何帮助。提前致谢

@Override
public View getView(final int position, View convertView, ViewGroup parent) {   
    final ArrayList<String> array=JournalArray.get(position);
    LayoutInflater inflator = ((Activity) activity).getLayoutInflater();
    final Hashtable<Integer, Integer> hash = new Hashtable<Integer, Integer>();
    final Button imgAR;
    final Button imgPDF;
    final Button imgDown;
    final Button btnDel;
    final TextProgressBar progress;
    //ConnectionDetector cd=new ConnectionDetector(activity);
    final Database db=new Database(activity);
    //final ImageView imgViewFlag;
    //final TextView txtViewTitle,txtProgress;

    if(convertView==null)
    {
        convertView = inflator.inflate(R.layout.familylist_item, null);
    }       
        progress=(TextProgressBar)convertView.findViewById(R.id.downprogress);
        progress.setTextColor(Color.BLACK);
        //progress.setTag(tag)
        //progress.setVisibility(View.GONE);
        TextView txtViewTitle = (TextView) convertView.findViewById(R.id.text);
        //view.txtProgress=(TextView)convertView.findViewById(R.id.textProgress);
        ImageView imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
         imgAR=(Button)convertView.findViewById(R.id.imageAR);
        //view.imgAR.setTag(view);
         imgDown=(Button)convertView.findViewById(R.id.imageDown);
        //view.imgDown.setTag(view);
         imgPDF=(Button)convertView.findViewById(R.id.imagePDF);
        //view.imgPDF.setTag(view);
        //view.progress=(ProgressBar)convertView.findViewById(R.id.downprogress);
        btnDel=(Button)convertView.findViewById(R.id.btnDel);
        //view.btnDel.setTag(view);
        //convertView.setTag(view);

    if(DeleteBin){
        if(XMLParser.CheckFileInSD(array.get(5))){
        btnDel.setVisibility(View.VISIBLE);
        imgAR.setVisibility(View.GONE);
        imgDown.setVisibility(View.GONE);
        imgPDF.setVisibility(View.GONE);
        progress.setVisibility(View.GONE);
        }
        else{
            imgDown.setVisibility(View.GONE);
            imgAR.setVisibility(View.GONE);
            btnDel.setVisibility(View.GONE);
            imgPDF.setVisibility(View.GONE);
            progress.setVisibility(View.GONE);
        }
    }
    else{
        btnDel.setVisibility(View.GONE);
        if(!XMLParser.CheckFileInSD(array.get(5))){
            imgAR.setVisibility(View.GONE);
            imgPDF.setVisibility(View.GONE);
            imgDown.setVisibility(View.VISIBLE);
            progress.setVisibility(View.GONE);
        }
        else{
            imgAR.setVisibility(View.VISIBLE);
            imgPDF.setVisibility(View.VISIBLE);
            imgDown.setVisibility(View.GONE);
            progress.setVisibility(View.GONE);
            }
        }

    txtViewTitle.setText(array.get(1));
    channelid=array.get(4);
    RelativeLayout.LayoutParams paramrel = new RelativeLayout.LayoutParams(width/5, height/5);
    paramrel.leftMargin = width/10;
    imgViewFlag.setLayoutParams(paramrel);
    imgViewFlag.setImageDrawable(Drawable.createFromPath(new File(array.get(3)).getAbsolutePath()));
    btnDel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            pos=position;
            Deletefiles(pos);
            Toast.makeText(activity, "Сэтгүүл амжилттай усгтгагдлаа", Toast.LENGTH_SHORT).show();
            btnDel.setVisibility(View.GONE);
        }
    });
    imgAR.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            channelid=array.get(4);
            Intent intent=new Intent(activity,ARELViewActivity.class);
            intent.putExtra("channelid", db.getConfigByChannelID(channelid));
            activity.startActivity(intent);
        }
    });
    imgPDF.setOnClickListener(new View.OnClickListener() {

        @SuppressLint("SdCardPath")
        @Override
        public void onClick(View v) {
            path=array.get(5);
            String PATH=Environment.getExternalStorageDirectory().getAbsolutePath()+ "/sdcard/.FamilyMagazine/";    
            openBook(PATH+XMLParser.getFileName(path));
        }
    });
    imgDown.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Database db=new Database(activity);
            final ConnectionDetector cd=new ConnectionDetector(activity);
            if(downloading<3){
            downloading++;

            path=array.get(5);
            channelid=array.get(4);
            pos=position;
            progress.setMax(100);
            progress.setProgress(0);
            progress.setVisibility(View.VISIBLE);
            imgDown.setVisibility(View.GONE);
            progress.setText("Татаж байна...");
            new AsyncTask<View, String, String>() {
                private View v;

                @Override
                protected String doInBackground(View... params) {
                    v = params[0];
                    int count;
                    isLoading=true;
                    //mprogress=progress;
                    String result="0";
                    if(cd.isConnectingToInternet()){
                        getChannel(channelid);
                        try {
                            URL url = new URL(path);
                            URLConnection conection = url.openConnection();
                            conection.connect();
                            // getting file length
                            lenghtOfFile = conection.getContentLength();

                            // input stream to read file - with 8k buffer
                            InputStream input = new BufferedInputStream(url.openStream(), 8192);

                            String PATH="/mnt/sdcard/sdcard/.FamilyMagazine";
                            File dir=new File(PATH);
                            boolean test=dir.mkdirs();
                            OutputStream output = new FileOutputStream("/mnt/sdcard/sdcard/.FamilyMagazine/"+XMLParser.getFileName(path));
                            byte data[] = new byte[1024];

                            long total = 0;

                            while ((count = input.read(data)) != -1) {
                                total += count;
                                // publishing the progress....
                                // After this onProgressUpdate will be called
                                publishProgress(""+(int)((total*100)/lenghtOfFile));

                                // writing data to file
                                output.write(data, 0, count);
                                hash.put(pos, (int)((total*100)/lenghtOfFile));
                            }

                            // flushing output
                            output.flush();

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


                        } catch (Exception e) {
                            result=e.toString();
                        }
                        }
                    else
                        result="Интернетэд холбогдоогүй байна!";

                    return result;
                }
                protected void onProgressUpdate(String... progresses) {
                    // setting progress percentage
                progress.setProgress(Integer.parseInt(progresses[0]));
                int x=lenghtOfFile/100 * Integer.parseInt(progresses[0])/1048576;
                progress.setText(x+"MB / "+lenghtOfFile/1048576+"MB");
                }
                @Override
                protected void onPostExecute(String result) {
                    super.onPostExecute(result);
                    //view.txtProgress.setVisibility(View.GONE);
                    downloading--;

                    isLoading=false;
                    if(result.equals("0")){
                        imgDown.setVisibility(View.GONE);
                        progress.setVisibility(View.GONE);
                        btnDel.setVisibility(View.GONE);
                        imgAR.setVisibility(View.VISIBLE);
                        imgPDF.setVisibility(View.VISIBLE);

                    }
                    else
                    {
                        Toast.makeText(activity, result, Toast.LENGTH_LONG).show();

                        imgDown.setVisibility(View.VISIBLE);
                        btnDel.setVisibility(View.GONE);
                        imgAR.setVisibility(View.GONE);
                        imgPDF.setVisibility(View.GONE);


                    }

         }
            }.execute(v);
            }
            else
                Toast.makeText(activity, "Өөр сэтгүүл татаж байна\n Түр хүлээнэ үү", Toast.LENGTH_LONG).show();
        }
    });

    return convertView;

}
4

1 回答 1

1

我遇到过这个问题。发生这种情况是因为 Listview 重用了视图(行),它不会为所有行创建视图,它只创建一些在屏幕上可见的行,其余行只是重用创建的视图。

您的问题的解决方案是维护某个自定义对象中每一行的状态(您可以拥有这些对象的数组列表),然后在 getView() 方法中,从该对象恢复所有视图的状态。 例如,您可以将此对象中progressBar 的状态保存为布尔值。一旦有人按下按钮,对象(对应于特定的行)应该更新为“真”布尔值。并且每次在获取视图中,都应该从此对象设置进度条的状态。

这应该可以解决您的问题。

于 2013-05-14T11:07:00.333 回答