2013-02-04 48 views
6

我有一個場景,在我的聊天應用程序中,我想發送照片,發送照片需要時間。我附加了一個進度條(水平)來顯示在我的應用程序中發送照片的進度。但我無法更新進度欄,因爲它是自定義列表視圖的項目。更新列表視圖中的進度欄​​

這是我的asynchtask設置進度欄的進度,但我無法得到它。

class SendPhotoToFriend extends AsyncTask<String, String, String>{ 

     String receiver,path = ""; 

     public SendPhotoToFriend(String path,String receiver) { 
      // TODO Auto-generated constructor stub 

      this.path = path; 
      this.receiver = receiver; 

     } 


     @Override 
     protected String doInBackground(String... arg0) { 
      // TODO Auto-generated method stub 

      ServiceDiscoveryManager sdm = ServiceDiscoveryManager 
      .getInstanceFor(connection); 

      if (sdm == null) 
       sdm = new ServiceDiscoveryManager(connection); 

      sdm.addFeature("http://jabber.org/protocol/disco#info"); 
      sdm.addFeature("jabber:iq:privacy"); 

      // Create the file transfer manager 
      FileTransferManager manager = new FileTransferManager(
        connection); 
      FileTransferNegotiator.setServiceEnabled(connection, true); 

      // Create the outgoing file transfer 
      OutgoingFileTransfer transfer = manager 
      .createOutgoingFileTransfer(receiver + "/Smack"); 

      System.out.println("Receiver of the file is "+receiver+"/smack"); 

      Log.i("transfere file", "outgoingfiletransfer is created"); 

      try { 
       long total = 0; 

       OutgoingFileTransfer.setResponseTimeout(30000); 
       transfer.sendFile(new File(path), "Description"); 
       Log.i("transfere file", "sending file"); 
       while (!transfer.isDone()) { 
        //Thread.sleep(1000); 

        total+=transfer.getProgress(); 

        publishProgress(""+(int)((total*100)/transfer.getFileSize())); 

        Log.i("transfere file", "sending file status " 
          + transfer.getStatus() + "progress: " 
          + transfer.getProgress()); 
        if (transfer.getStatus() == org.jivesoftware.smackx.filetransfer.FileTransfer.Status.error) { 
         Log.i("transfere file", "Errorrr isss: " 
           + transfer.getError()+transfer.getPeer()); 
         transfer.cancel(); 
         break; 
        } 
       } 

      } catch (XMPPException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      Log.i("transfere file", "sending file done"); 

      return null; 
     } 

     @Override 
     protected void onProgressUpdate(final String... values) { 
      // TODO Auto-generated method stub 
      super.onProgressUpdate(values); 


      LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      final View view =inflater.inflate(R.layout.user_chat_send_chat, null); 
      ProgressBar p_Bar = (ProgressBar)view.findViewById(R.id.progress_bar_image); 
      p_Bar.setProgress(Integer.parseInt(values[0])); 

      customAdapter1.notifyDataSetChanged(); 



     } 

     @Override 
     protected void onPostExecute(String result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
     } 
    } 

進度條是自定義列表視圖的視圖,我想在運行時更新它。

謝謝

回答

7

你在onProgress回調中膨脹ProgressBar。這是完全錯誤的。 ProgressBar應該在你的AsyncTask啓動(膨脹和添加)之前被添加到視圖中,然後在你的AsyncTask構造函數中將它變爲visibile。您還應該在AsyncTask中存儲對ProgressBar的引用(或者通過非靜態內部類在其他範圍內)。

在你的AsyncTask應該尋找這樣結束(假設進度已經連接到視圖):

class SendPhotoToFriend extends AsyncTask<String, String, String> { 

    String receiver,path = ""; 

    ProgressBar progress; 

    public SendPhotoToFriend(String path, String receiver, ProgressBar progress) { 
     this.path = path; 
     this.receiver = receiver; 
     this.progress = progress; 
     this.progress.setVisibility(View.Visible); 
    } 


    @Override 
    protected String doInBackground(String... arg0) { 
     // You're application logic... 
     // ... 
     publishProgress((int)((total*100)/transfer.getFileSize())); 
     // ... 
    } 

    @Override 
    protected void onProgressUpdate(final Integer... values) { 
     this.progress.setProgress(values[0]); 
    } 
} 

而且你不需要(也不應該)在調用adapter.notifiyDataSetChanged()您onProgres回調。

+0

我應該從getView方法調用它嗎? –

+0

這個決定很大程度上取決於您的應用程序的軟件體系結構。沒有錯或正確的。這更像是將它稱爲AsyncTask更合理的地方..恐怕我無法爲你回答這個問題。 – Taig

+0

好的,謝謝。我會檢查它並將恢復爲你 –

0

我想每個項目視圖有一個進度條,如果是這樣的話,你可以使用getView以獲取該位置的視圖,然後撥打:

 View view = customAdapter1.getView(position, convertView, parent); 
     ProgressBar p_Bar = (ProgressBar)view.findViewById(R.id.progress_bar_image); 
     p_Bar.setProgress(Integer.parseInt(values[0])); 

不需要調用customAdapter1。 notifyDataSetChanged();