如果 ViewHolder 的语句每次都返回 false
If statement for ViewHolder is returning false all everytime
回到另一个 android 问题,感觉好像我在这里遗漏了一些简单的东西。
viewholder 获取从中读取结果的 table 的特定字段中的内容。
现在我想将该结果与 char 进行比较以更改其所在布局的颜色,但不幸的是没有按计划工作。
持有人的代码如下:
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
ViewHolder holder = (ViewHolder) view.getTag();
if (holder == null) {
holder = new ViewHolder();
holder.colImp = cursor.getColumnIndexOrThrow(BetDbAdapter.COL_OUTCOME);
holder.listTab = view.findViewById(R.id.won_lost);
view.setTag(holder);
}
if (cursor.getString(holder.colImp).trim() == "W" || cursor.getString(holder.colImp).trim() == "w") {
holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.green));
} else {
holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.red));
}
}
if 语句一直返回红色,即使内容是 "W",这是应用程序的屏幕:
这也是插入内容,目前出于测试目的对其进行了硬编码:
mDbAdapter.createBet("W", 12.34, 14.78);
mDbAdapter.createBet("W", 23.77, 1.90);
mDbAdapter.createBet("W", 123.45, 134);
mDbAdapter.createBet("W", 0.4, 1.34);
请看看你是否能看到我无法解决的问题。
字符串是一个对象,所以相等比较器只测试引用而不是值。
要测试字符串相等性,您应该使用
cursor.getString(holder.colImp).trim().equal("W");
问题在
cursor.getString(holder.colImp).trim() == "W" || cursor.getString(holder.colImp).trim() == "w"
==
等式仅适用于原始值,或检查对象是否相同reference
。
尝试以下
cursor.getString(holder.colImp).trim().equalsIgnoreCase("W")
您在代码中比较字符串,而不是字符。字符 - 'W'。字符串 - "W"
如果您需要比较字符:
cursor.getString(holder.colImp).trim().charAt(0) == 'W'
但更容易比较字符串
"W".equals(otherString);
回到另一个 android 问题,感觉好像我在这里遗漏了一些简单的东西。
viewholder 获取从中读取结果的 table 的特定字段中的内容。
现在我想将该结果与 char 进行比较以更改其所在布局的颜色,但不幸的是没有按计划工作。
持有人的代码如下:
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
ViewHolder holder = (ViewHolder) view.getTag();
if (holder == null) {
holder = new ViewHolder();
holder.colImp = cursor.getColumnIndexOrThrow(BetDbAdapter.COL_OUTCOME);
holder.listTab = view.findViewById(R.id.won_lost);
view.setTag(holder);
}
if (cursor.getString(holder.colImp).trim() == "W" || cursor.getString(holder.colImp).trim() == "w") {
holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.green));
} else {
holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.red));
}
}
if 语句一直返回红色,即使内容是 "W",这是应用程序的屏幕:
这也是插入内容,目前出于测试目的对其进行了硬编码:
mDbAdapter.createBet("W", 12.34, 14.78);
mDbAdapter.createBet("W", 23.77, 1.90);
mDbAdapter.createBet("W", 123.45, 134);
mDbAdapter.createBet("W", 0.4, 1.34);
请看看你是否能看到我无法解决的问题。
字符串是一个对象,所以相等比较器只测试引用而不是值。
要测试字符串相等性,您应该使用
cursor.getString(holder.colImp).trim().equal("W");
问题在
cursor.getString(holder.colImp).trim() == "W" || cursor.getString(holder.colImp).trim() == "w"
==
等式仅适用于原始值,或检查对象是否相同reference
。
尝试以下
cursor.getString(holder.colImp).trim().equalsIgnoreCase("W")
您在代码中比较字符串,而不是字符。字符 - 'W'。字符串 - "W"
如果您需要比较字符:
cursor.getString(holder.colImp).trim().charAt(0) == 'W'
但更容易比较字符串
"W".equals(otherString);