0

我目前使用两个不同的片段来显示一半 Lis​​t :

片段 1

if (i%2 != 1) {
Show first half List;
}

片段 2

if (i%2 == 1) {
Show second half List;
}

但我想要一个更清洁的片段,其中一个片段被称为两次:

分段

prefOperator = bundle.getInt(WHICH_HALF);
if (prefOperator == 1) OPERATOR = "!=";
else if (prefOperator == 2) OPERATOR = "==";

if (i%2 OPERATOR 1) {
Show List;
}

我想这是可能的,但找不到怎么做。

4

1 回答 1

4

不要更改运算符,而是更改您要比较的值:

// Since WHICH_HALF has value 1 or 2, we will subtract one.
int prefOperator = bundle.getInt(WHICH_HALF) - 1;

if (i%2 == prefOperator) {
  // Show first half of list
} else {
  // Show second half of list
}
于 2013-03-15T10:31:12.453 回答