2013-05-13 70 views
1

我在列表視圖中有按鈕和進度條(不可見)。當按鈕單擊它應該消失,進度條應該是可見的並開始運行(從網絡服務器下載),當它完成與運行按鈕應該再次出現。現在,當我點擊第一個項目的按鈕時,進度條會運行,但如果我向下滾動,直到第一個項目熄滅屏幕,我會看到進度條與第一個項目的進度條同時運行在最後一項listview上。如果我向上滾動第一個項目的進度條正常運行。如果我點擊第二項,listview的倒數第二項也是一樣的。什麼是問題,我怎麼解決它?請幫忙!!!當listview向下滾動時,新的listview項會複製第一個listview項的動作嗎?

這裏是我的自定義列表視圖適配器異步任務

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; 

}

回答

0

因爲要使用視圖保持者,意見正在重新用作您向下滾動。當使用視圖持有者時,如果要在特定行中更改視圖,則無法假定視圖以特定狀態(「通過」,「可見」等)開始。您應該每次明確設置您的視圖的狀態。

+0

請告訴我應該在哪裏設置我的觀點狀態。我如何避免重複使用? – casper 2013-05-13 22:53:32

+0

問題僅在使用asynctask的下載過程中發生。一切工作正常,除非在下載時向下滾動,我看到重複。 – casper 2013-05-13 23:24:56

+0

我將所有ViewHolder都帶走了,而且我仍然看到同樣的問題 – casper 2013-05-13 23:52:24