2016-02-29 39 views
2

我想使用此代碼使用標籤沒有循環:在onBindviewHolder

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) { 
    label :{ 
     if(some condition) { 
      //my code 
     } else { 
      if(my condition) { 
       //some code 
       continue label; // from here i want to go back to label, how to i go? 
      } 
     } 
    } 
} 

行繼續標籤;給我這個錯誤:不是一個循環標籤

所以我需要回到行「標籤」我該怎麼做?

+0

你試過我發佈的while循環解決方案嗎? – Bandreid

+0

它不工作,因爲它是在綁定視圖 –

+0

你能更好地解釋然後你想達到什麼?如果你想以編程方式調用onBindViewHolder()方法,你可以使用bindVIewHolder()來完成。更多在http://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#onBindViewHolder(VH,int) – Bandreid

回答

2

爲什麼你必須使用「標籤」循環?

嘗試使用while循環,如下所示:

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) { 
    while (true) { 
     if (some condition) { 
      // my code 
      break; // if this code ran then exit the while loop 
     } else if (my condition) { 
      // some code 
      continue; // from here it will make another iteration in the while loop 
     } 
    } 
} 
+0

但這是在onbindviewholder,我怎麼能把它放在while循環? –

+0

@ParthAnjaria我編輯的代碼,使其更清晰。您只需將on循環放入onBindViewHolder()方法內。還有其他的東西不清楚嗎? – Bandreid

0

您可以使用繼續用於跳過循環迭代關鍵字

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) { 
    while (true) { 
     if(some condition) { 
      //my code 
     } else { 
      if(my condition) { 
       //some code 
       continue ; // skipping this iteration 
      } 
     } 
    } 
} 
相關問題