Android SmartTable根据int状态格式化文字及颜色

Android SmartTable根据int状态格式化文字及颜色_第1张图片

private void initData() {
        List list = new ArrayList<>();
        list.add(new UserInfo("一年级", "李同学", 6, 1, 120, 1100, 450, 0));
        list.add(new UserInfo("一年级", "张同学", 6, 2, 120, 1100, 450, 1));
        list.add(new UserInfo("一年级", "王同学", 7, 3, 120, 1100, 450, 2));
        list.add(new UserInfo("一年级", "闻同学", 7, 4, 120, 1100, 450, 0));
        list.add(new UserInfo("二年级", "李同学", 6, 5, 120, 1100, 450, 1));
        list.add(new UserInfo("二年级", "李同学", 6, 6, 120, 1100, 450, 2));
        list.add(new UserInfo("二年级", "李同学", 7, 7, 120, 1100, 450, 0));
        list.add(new UserInfo("二年级", "李同学", 7, 8, 120, 1100, 450, 1));
        list.add(new UserInfo("三年级", "李同学", 6, 9, 120, 1100, 450, 2));
        list.add(new UserInfo("三年级", "李同学", 6, 10, 120, 1100, 450, 0));
        list.add(new UserInfo("三年级", "李同学", 7, 11, 120, 1100, 450, 1));
        list.add(new UserInfo("三年级", "李同学", 7, 12, 120, 1100, 450, 2));

        //创建列
        Column classColumn = new Column("班级", "classes"); //classes为字段名
        cityColumn.setAutoMerge(true);
        Column nameColumn = new Column("姓名", "name");
        Column ageColumn = new Column("年龄", "age");
        Column indexColumn = new Column("序号", "index");
        Column heightColumn = new Column("身高", "height");
        Column addressColumn = new Column("住址", "address");
        Column phoneColumn = new Column("电话", "phone");
        Column stateColumn = new Column("状态", "state", new IFormat() {
            @Override
            public String format(Integer state) {
                String str = "";
                if (state == 0) {
                    str = "未开始";
                } else if (state == 1) {
                     str = "进行中";
                } else if (state == 2) {
                     str = "已完成";
                }
                return str;
            }
        }, new IDrawFormat() {
            @Override
            public int measureWidth(Column column, int position, TableConfig config) {
                return 0;
            }

            @Override
            public int measureHeight(Column column, int position, TableConfig config) {
                return 0;
            }

            @Override
            public void draw(Canvas c, Rect rect, CellInfo cellInfo, TableConfig config) {
                Paint paint = config.getPaint();
                if (cellInfo.value.equals("未开始")) {
                    paint.setColor(ContextCompat.getColor(context, R.color.red));
                } else if (cellInfo.value.equals("进行中")) {
                    paint.setColor(ContextCompat.getColor(context, R.color.gray));
                } else if (cellInfo.value.equals("已完成")) {
                    paint.setColor(ContextCompat.getColor(context,R.color.colorPrimary));
                }
                paint.setStyle(Paint.Style.FILL);
                c.save();
                c.clipRect(rect);
                c.drawText(cellInfo.value, rect.right - 60, rect.bottom - 15, paint);
                c.restore();
            }
        });

        final TableData tableData = new TableData<>("测试", list, classColumn ,
                nameColumn, ageColumn, indexColumn, heightColumn, addressColumn, phoneColumn, stateColumn);

        table.setTableData(tableData);

    }

com.github.huangyanbin:SmartTable:"2.2.0"

你可能感兴趣的:(android,windows)