我的 bindView 中的开关不是 运行 预期的情况
A switch in my bindView is not running the expected cases
我正在尝试使用一个 ImageView
和来自 SQLite 数据库的三个 TextView 来填充一个列表。 TextView
中的数据是我期望的,但 ImageView
永远不会改变。
ImageView
应该根据光标在 x 行和 y 列中的值在两个图标之间交换。该列填充了 1
或 0
的 Integer
。如果第 y 列@第 x 行为 1,则红色图像应填充 ImageView
。如果为 0,则绿色图像应该。
这似乎永远不会发生。它总是红色图像,logcat 总是表示光标的值为 1。
04-14 08:24:04.188: A/example.Whosebug.MainActivity@1840e274(6510):
04-14 08:24:04.191: D/cursor@iconHolder(6510): 1
04-14 08:24:04.191: D/icon(6510): true 1
04-14 08:24:04.191: V/date(6510): 2015-04-14 becomes April, 14
04-14 08:24:04.192: A/example.Whosebug.MainActivity@1840e274(6510):
04-14 08:24:04.193: D/cursor@iconHolder(6510): 1
04-14 08:24:04.194: D/icon(6510): true 1
这是 SQLite 数据库的片段。
_id, weight, height, body_mass_index, icon, date
"1","45.359233334609435","1778.003556007112","14.348300215310468","0","2015-04-14"
"2","47.627195001339906","1778.003556007112","15.06571522607599","1","2015-04-14"
"3","45.359233334609435","1778.003556007112","14.348300215310468","0","2015-04-14"
如您所见,图标本应先绿后红再绿,但三次都是红色。如果能帮我弄清楚为什么我的代码不起作用,我真的很感激。这是我的 bindView
.
@Override
public void bindView(View view, Context context, Cursor cursor) {
ImageView iconView = (ImageView) view.findViewById(R.id.richListIcon);
TextView weightInfo = (TextView) view.findViewById(R.id.personal_item1);
TextView bmiInfo = (TextView) view.findViewById(R.id.personal_item2);
TextView dateInfo = (TextView) view.findViewById(R.id.timestamp_item);
int indexWeight;
int indexBmi;
int indexDate;
String weightHolder = null;
String weightModified = null;
String bmiHolder = null;
String bmiModified = null;
String dateHolder = null;
String upDateHolder = null;
int iconHolder = 3;
int iconSet;
if (cursor != null) {
Log.wtf(String.valueOf(getActivity()), "not null");
iconHolder = (cursor.getColumnIndex
(DatabaseAdapter.DataManager.publicIconSetter));
Log.d("cursor@iconHolder", String.valueOf(cursor.getColumnIndexOrThrow
(DatabaseAdapter.DataManager.publicIconSetter)));
weightHolder = (String.valueOf
(cursor.getDouble(cursor.getColumnIndex
(DatabaseAdapter.DataManager.publicWeight))));
bmiHolder = (String.valueOf
(cursor.getDouble(cursor.getColumnIndex
(DatabaseAdapter.DataManager.publicBMI))));
dateHolder = (String.valueOf
(cursor.getString(cursor.getColumnIndex
(DatabaseAdapter.DataManager.publicDate))));
}
switch (iconHolder){
case 0:
iconSet = R.mipmap.green_icon;
Log.d("icon", "false " + iconHolder);
break;
case 1:
Log.d("icon", "true " + iconHolder);
iconSet = R.mipmap.red_icon;
break;
default: iconSet = R.mipmap.ic_launcher;
Log.wtf("IVF_IconSetter", "Error, value from table is " + iconHolder);
}
iconView.setImageResource(iconSet);
...
}
还有我的十字军
public Cursor getAllData(){
SQLiteDatabase database = dataManager.getReadableDatabase();
String[] columns = {DataManager.UID,
DataManager.Icon_Gained_Weight,
DataManager.Weight,
DataManager.BMI,
DataManager.Date
};
String orderBy = DataManager.UID + " DESC";
Cursor cursor = null;
try {
cursor = database.query(DataManager.TABLE_NAME, columns, null, null, null, null, orderBy);
if(cursor != null){
cursor.moveToFirst();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return cursor;
}
Playmaker420已经解决了这个问题。如果Playmaker420选择不这样做,我会写出答案。
方法getColumnIndex() returns the index/position of the column and not its value. In your case, the value returned is always "1" since it is the second column (as you can see in the columns array). You should use the getInt()方法:
iconHolder = (cursor.getInt(cursor.getColumnIndex(/** here goes the column name */)));
System.out.println(cursor.getString(cursor.getColumnIndex("icon"));
这应该打印列的值 "icon"
我正在尝试使用一个 ImageView
和来自 SQLite 数据库的三个 TextView 来填充一个列表。 TextView
中的数据是我期望的,但 ImageView
永远不会改变。
ImageView
应该根据光标在 x 行和 y 列中的值在两个图标之间交换。该列填充了 1
或 0
的 Integer
。如果第 y 列@第 x 行为 1,则红色图像应填充 ImageView
。如果为 0,则绿色图像应该。
这似乎永远不会发生。它总是红色图像,logcat 总是表示光标的值为 1。
04-14 08:24:04.188: A/example.Whosebug.MainActivity@1840e274(6510):
04-14 08:24:04.191: D/cursor@iconHolder(6510): 1
04-14 08:24:04.191: D/icon(6510): true 1
04-14 08:24:04.191: V/date(6510): 2015-04-14 becomes April, 14
04-14 08:24:04.192: A/example.Whosebug.MainActivity@1840e274(6510):
04-14 08:24:04.193: D/cursor@iconHolder(6510): 1
04-14 08:24:04.194: D/icon(6510): true 1
这是 SQLite 数据库的片段。
_id, weight, height, body_mass_index, icon, date
"1","45.359233334609435","1778.003556007112","14.348300215310468","0","2015-04-14"
"2","47.627195001339906","1778.003556007112","15.06571522607599","1","2015-04-14"
"3","45.359233334609435","1778.003556007112","14.348300215310468","0","2015-04-14"
如您所见,图标本应先绿后红再绿,但三次都是红色。如果能帮我弄清楚为什么我的代码不起作用,我真的很感激。这是我的 bindView
.
@Override
public void bindView(View view, Context context, Cursor cursor) {
ImageView iconView = (ImageView) view.findViewById(R.id.richListIcon);
TextView weightInfo = (TextView) view.findViewById(R.id.personal_item1);
TextView bmiInfo = (TextView) view.findViewById(R.id.personal_item2);
TextView dateInfo = (TextView) view.findViewById(R.id.timestamp_item);
int indexWeight;
int indexBmi;
int indexDate;
String weightHolder = null;
String weightModified = null;
String bmiHolder = null;
String bmiModified = null;
String dateHolder = null;
String upDateHolder = null;
int iconHolder = 3;
int iconSet;
if (cursor != null) {
Log.wtf(String.valueOf(getActivity()), "not null");
iconHolder = (cursor.getColumnIndex
(DatabaseAdapter.DataManager.publicIconSetter));
Log.d("cursor@iconHolder", String.valueOf(cursor.getColumnIndexOrThrow
(DatabaseAdapter.DataManager.publicIconSetter)));
weightHolder = (String.valueOf
(cursor.getDouble(cursor.getColumnIndex
(DatabaseAdapter.DataManager.publicWeight))));
bmiHolder = (String.valueOf
(cursor.getDouble(cursor.getColumnIndex
(DatabaseAdapter.DataManager.publicBMI))));
dateHolder = (String.valueOf
(cursor.getString(cursor.getColumnIndex
(DatabaseAdapter.DataManager.publicDate))));
}
switch (iconHolder){
case 0:
iconSet = R.mipmap.green_icon;
Log.d("icon", "false " + iconHolder);
break;
case 1:
Log.d("icon", "true " + iconHolder);
iconSet = R.mipmap.red_icon;
break;
default: iconSet = R.mipmap.ic_launcher;
Log.wtf("IVF_IconSetter", "Error, value from table is " + iconHolder);
}
iconView.setImageResource(iconSet);
...
}
还有我的十字军
public Cursor getAllData(){
SQLiteDatabase database = dataManager.getReadableDatabase();
String[] columns = {DataManager.UID,
DataManager.Icon_Gained_Weight,
DataManager.Weight,
DataManager.BMI,
DataManager.Date
};
String orderBy = DataManager.UID + " DESC";
Cursor cursor = null;
try {
cursor = database.query(DataManager.TABLE_NAME, columns, null, null, null, null, orderBy);
if(cursor != null){
cursor.moveToFirst();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return cursor;
}
Playmaker420已经解决了这个问题。如果Playmaker420选择不这样做,我会写出答案。
方法getColumnIndex() returns the index/position of the column and not its value. In your case, the value returned is always "1" since it is the second column (as you can see in the columns array). You should use the getInt()方法:
iconHolder = (cursor.getInt(cursor.getColumnIndex(/** here goes the column name */)));
System.out.println(cursor.getString(cursor.getColumnIndex("icon"));
这应该打印列的值 "icon"