简体   繁体   中英

A switch in my bindView is not running the expected cases

I am trying to populate a list list with an ImageView , and three TextViews from an SQLite database. The data in the TextView s is what i expect it to be, but the ImageView never changes.

The ImageView is supposed to swap between two icons depending on the value of the Cursor at x row in y column. The column is populated with an Integer of 1 or 0 . If column y @ row x is 1 the red image should populate the ImageView . If 0 then the green image should.

That never seems to happen. It is always the red image, and the logcat always says the the value of cursor is 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

Here is a snippet of the SQLite database.

_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"

As you can see the icon should have been green then red and then green, but it was just red all three times. I really would appreciate some help with figuring out why my code doesn't work. Here is my 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);

    ...

    }

And my Crusor

 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;
}

This problem has been solved by Playmaker420. I'll write out the answer if Playmaker420 chooses not to do so.

The method 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() method:

iconHolder = (cursor.getInt(cursor.getColumnIndex(/** here goes the column name */)));
System.out.println(cursor.getString(cursor.getColumnIndex("icon")); 

这应该打印列“ icon”的值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM