2012-04-11 23 views
0

(對不起,我在拼寫錯誤)組合服務,進度,GridView的(和他notifydatasetchanged)的QuickAction,實現出錯

我有一些事情的組合存在問題:

我用一個GridView從數據庫下載顯示內容。 onClick文件被下載,以後可以查看。它大約20MB。此下載由服務完成。

gridView中的每個項目都在佈局中包含一個進度條。如果該項目在後臺通過該服務重新加載,則會顯示此進度條。

服務發送一個關於下載進度的廣播,Intent包含在gridview的適配器的數據中找到它的項目的ID。

一個BroadcastReceiver IST在我的活動,以獲得最新進展註冊(記住,同時下載是可能的),這就要求在GridView適配器

public void setProgress(long id, int progress) 
{ 
    for (int i = 0;i < list.size();i++) 
    { 
     if (list.get(i).getId() == id) 
     { 
      list.get(i).setProgress(progress); 
      notifyDataSetChanged(); 
     } 
    }   
} 

功能「setProgress」對於這一點,一切工作正常。

此外,我使用QuickAction實現從http:// www。 londatiga.net/it/how-to-create-quickaction-dialog-in-android/(空格,因爲我不能發佈兩個以上的超鏈接)

現在問題來了:

有時候,我猜notifydatasetchanged時被調用並且用戶點擊一個項目時,快速反應被顯示在錯誤的位置上。

爲了說明清楚這裏有兩張照片:

第一個是應該發生什麼(在這種情況下,在GridView上的第一項點擊) http://dl.dropbox.com/u/9031500/expected.png

第二張圖片顯示會發生什麼有時(只有當一些下載正在運行,這就是爲什麼我猜測它是因爲「notifydatasetchanged」和視圖的重建)。這也是第一項的點擊,不幸的是,快速動作顯示第四項: http://dl.dropbox.com/u/9031500/wrong.png

這是我在我的活動實現了快速動作的調用:

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    showQuickAction(view, position); 
} 

@Override 
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
    showQuickAction(view, position); 
    return true; 
} 


private void showQuickAction(View view, int position) 
{ 
    RelativeLayout facsimile = (RelativeLayout) view.findViewById(R.id.lib_item_image_layout); 
    Log.i(LOGTAG, "Position:"+position); 
    currentPaper = TZReader.paperDao.load(libraryAdapter.getItemId(position)); 
    Log.i(LOGTAG,"Set CurrentPaper:"+currentPaper.getTitelWithDate()); 

    ActionItem downloadItem = new ActionItem(ACTION_ITEM_DOWNLOAD, "Download", getResources().getDrawable(R.drawable.ic_action_download)); 
    ActionItem readItem = new ActionItem(ACTION_ITEM_READ, "Lesen", getResources().getDrawable(R.drawable.ic_action_read)); 
    ActionItem deleteItem = new ActionItem(ACTION_ITEM_DELETE, "Löschen", getResources().getDrawable(R.drawable.ic_action_delete)); 
    ActionItem cancelItem = new ActionItem(ACTION_ITEM_CANCEL, "Abbrechen", getResources().getDrawable(R.drawable.ic_action_cancel)); 

    QuickAction mQuickAction = new QuickAction(this, QuickAction.HORIZONTAL); 

    switch (currentPaper.getState()) 
    { 
    case Paper.DOWNLOADED_READABLE: 
     mQuickAction.addActionItem(readItem); 
     mQuickAction.addActionItem(deleteItem);   
     break; 
    case Paper.DOWNLOADED_BUT_UPDATE: 
     mQuickAction.addActionItem(downloadItem); 
     mQuickAction.addActionItem(deleteItem); 
     break; 
    case Paper.IS_DOWNLOADING: 
     mQuickAction.addActionItem(cancelItem); 
     break; 
    case Paper.NOT_DOWNLOADED: 
     mQuickAction.addActionItem(downloadItem); 
     break; 
    } 


    //Set listener for action item clicked 
    mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {   
     @Override 
     public void onItemClick(QuickAction source, int pos, int actionId) {       
      //here we can filter which action item was clicked with pos or actionId parameter 
      switch(actionId) 
      { 
      case ACTION_ITEM_DOWNLOAD: 
       Intent downloadIntent = new Intent(getApplicationContext(), DownloadService.class); 
       downloadIntent.putExtra(DownloadService.PARAMETER_PAPER_DB_ID_LONG, currentPaper.getId()); 
       startService(downloadIntent); 
       break; 
      case ACTION_ITEM_READ: 

       break; 
      case ACTION_ITEM_DELETE: 
       DeleteAlertDialogFragment newFragment = DeleteAlertDialogFragment.newInstance(currentPaper.getId()); 
       newFragment.setStyle(SherlockDialogFragment.STYLE_NO_TITLE,0); 
       newFragment.show(getSupportFragmentManager(), "dialog"); 
       break; 
      case ACTION_ITEM_CANCEL: 
       break;     
      } 
     } 
    });  



    mQuickAction.show(facsimile); 
} 

也許有人對我有任何想法或暗示,我該如何處理這個問題! 非常感謝!

回答

0

我找到了我的問題的解決方案。

解決方案是我實現自己的ProgressBar,它包含現在一個BroadcastListerner並設置每個項目的進度。所以我可以改變這個值,而不需要調用「notifydatasetchanged」。完美滿足我的需求。如果這是一個好的解決方案,我仍然不會表現出色,但它運作良好。

下面是進度代碼:

public class ListeningProgressBar extends ProgressBar { 

public ListeningProgressBar(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 


final IntentFilter intentDownloadProgressFilter = new IntentFilter(DownloadService.DOWNLOAD_PROGRESS_BROADCAST); 
private long paperId = 0; 
private boolean isReceiverRegistered = false; 

@Override 
protected void onAttachedToWindow() { 

    getContext().getApplicationContext().registerReceiver(downloadProgressBroadcastReceiver,intentDownloadProgressFilter); 
    isReceiverRegistered = true; 

    super.onAttachedToWindow(); 
} 

@Override 
protected void onDetachedFromWindow() { 
    if (isReceiverRegistered) 
    { 
     getContext().getApplicationContext().unregisterReceiver(downloadProgressBroadcastReceiver); 
     isReceiverRegistered = false; 
    } 
    super.onDetachedFromWindow(); 
} 

private BroadcastReceiver downloadProgressBroadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     long id = intent.getLongExtra(DownloadService.PARAMETER_PAPER_DB_ID_LONG, -1); 
     int progressValue = intent.getIntExtra(DownloadService.PARAMETER_PAPER_PROGRESS_INT, 0); 
     if (paperId == id) 
     { 
      setProgress(progressValue); 
     } 
    } 
}; 

public long getPaperId() { 
    return paperId; 
} 

public void setPaperId(long paperId) { 
    this.paperId = paperId; 
} 

} 

我只是用它作爲我的XML佈局正常的自定義視圖。 在適配器中,我設置了我的內容的ID,以便爲接收器提供setprogress數據,只要它的內容正確。

最後,我的問題解決了,無需調用notifydatasetchanged就可以更新進度。是啊!

+0

你好伴侶,我有一個有點類似的要求..可以請你指導我應該從哪裏廣播,以便我可以在我的customProgressbar類中使用它。我正在使用服務下載數據....我需要顯示它的狀態,就像你的要求..請幫助... – 2012-12-12 07:26:19

+0

你好伴侶,你能幫我解釋我的上面的評論嗎?...我如何設置每個progess欄的paperID?經歷了艱難的時間...... – 2012-12-18 11:04:30