0

我正在尝试ImageView使用 SQLite 数据库中的一个和三个 TextView 填充一个列表列表。s 中的数据TextView是我所期望的,但ImageView永远不会改变。

应该根据ImageViewy 列中 x 行的光标值在两个图标之间交换。该列填充了Integerof10。如果列 y@row x 为 1,则红色图像应填充ImageView. 如果为 0,则应为绿色图像。

这似乎永远不会发生。它总是红色的图像,logcat 总是说 cursor 的值为 1。

04-14 08:24:04.188: A/example.stackoverflow.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.stackoverflow.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 选择不这样做,我会写出答案。

4

2 回答 2

0

getColumnIndex()方法返回列的索引/位置,而不是它的值。在您的情况下,返回的值始终为“1”,因为它是第二列(如您在columns数组中所见)。您应该使用getInt()方法:

iconHolder = (cursor.getInt(cursor.getColumnIndex(/** here goes the column name */)));
于 2015-04-14T14:06:47.370 回答
0
System.out.println(cursor.getString(cursor.getColumnIndex("icon")); 

这应该打印列“图标”的值

于 2015-04-14T14:15:23.063 回答