2016-02-29 23 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 
      } 
     } 
    } 
} 
相关问题