1

我在列表视图中有按钮和进度条(不可见)。当按钮单击它应该消失并且进度条应该可见并开始运行(从网络服务器下载),当它完成时运行按钮应该再次出现。现在,当我单击第一项的按钮时,进度条会运行,但是如果我向下滚动直到第一项离开屏幕,我会看到进度条与列表视图的最后一项上的第一项的进度条同时运行。如果我向上滚动第一项的进度条运行正常。如果我单击第二个项目,列表视图的倒数第二个项目也会发生同样的情况。有什么问题,我该如何解决?请帮忙!!!

这是我的带有异步任务的自定义列表视图适配器

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

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

}
else
{
    view = (ViewHolder) convertView.getTag();
}

if(!isLoading){
if(DeleteBin){
    if(XMLParser.CheckFileInSD(array.get(5))){
    view.btnDel.setVisibility(View.VISIBLE);
    view.imgAR.setVisibility(View.GONE);
    view.imgDown.setVisibility(View.GONE);
    view.imgPDF.setVisibility(View.GONE);
    view.progress.setVisibility(View.GONE);
    }
    else{
        view.imgDown.setVisibility(View.GONE);
        view.imgAR.setVisibility(View.GONE);
        view.btnDel.setVisibility(View.GONE);
        view.imgPDF.setVisibility(View.GONE);
        view.progress.setVisibility(View.GONE);
    }
}
else{
    view.btnDel.setVisibility(View.GONE);
    if(!XMLParser.CheckFileInSD(array.get(5))){
        view.imgAR.setVisibility(View.GONE);
        view.imgPDF.setVisibility(View.GONE);
        view.imgDown.setVisibility(View.VISIBLE);
        view.progress.setVisibility(View.GONE);
    }
    else{
        view.imgAR.setVisibility(View.VISIBLE);
        view.imgPDF.setVisibility(View.VISIBLE);
        view.imgDown.setVisibility(View.GONE);
        view.progress.setVisibility(View.GONE);
    }
}
}
    view.txtViewTitle.setText(array.get(1));
    channelid=array.get(4);
    RelativeLayout.LayoutParams paramrel = new RelativeLayout.LayoutParams(width/5,      height/5);
    paramrel.leftMargin = width/10;
    view.imgViewFlag.setLayoutParams(paramrel);
    view.imgViewFlag.setImageDrawable(Drawable.createFromPath(new   File(array.get(3)).getAbsolutePath()));       
   view.imgDown.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if(downloading<1){
        downloading++;
        path=array.get(5);
        channelid=array.get(4);
        pos=position;
        view.progress.setMax(100);
        view.progress.setProgress(0);
        view.progress.setVisibility(View.VISIBLE);
        view.imgDown.setVisibility(View.GONE);
        view.progress.setText("Татаж байна...");
        new AsyncTask<ViewHolder, String, String>() {
            private ViewHolder v;

            @Override
            protected String doInBackground(ViewHolder... params) {
                v = params[0];
                int count;
                isLoading=true;
                //mprogress=progress;
                String result="0";
                if(view.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);
                        }

                        // flushing output
                        output.flush();

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


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

                return result;
            }
            protected void onProgressUpdate(String... progress) {
                // setting progress percentage
            view.progress.setProgress(Integer.parseInt(progress[0]));
            int x=lenghtOfFile/100 * Integer.parseInt(progress[0])/1048576;
            view.progress.setText(x+"MB / "+lenghtOfFile/1048576+"MB");
            }
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                //view.txtProgress.setVisibility(View.GONE);
                downloading--;
               // if (v.position == pos) {
                isLoading=false;
                if(result.equals("0")){
                    view.imgDown.setVisibility(View.GONE);
                    view.progress.setVisibility(View.GONE);
                    view.btnDel.setVisibility(View.GONE);
                    view.imgAR.setVisibility(View.VISIBLE);
                    view.imgPDF.setVisibility(View.VISIBLE);

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

                    view.imgDown.setVisibility(View.VISIBLE);
                    view.btnDel.setVisibility(View.GONE);
                    view.imgAR.setVisibility(View.GONE);
                    view.imgPDF.setVisibility(View.GONE);                           
                }
            }
          //}
        }.execute(view);
        }

    }
});

return convertView;

}

4

1 回答 1

0

因为您使用的是视图持有者,所以当您向下滚动时,视图会被重新使用。使用视图持有者时,如果您在某些行中更改它,则不能假设您的视图以特定状态(GONE、VISIBLE 等)开始。您应该每次都明确设置视图的状态。

于 2013-05-13T22:41:05.033 回答