【Android 个人理解(七)】用ContentValues和自定义Application处理数据

我当时面临的需求是
1、核心算法数据和UI的交互,2、多个数组数据的封装,3、不确定的数组数据转化到表格形式。

就于以上需求,我采用自定义的Application储存和传递全局的数据,采用ContentValues的key-value转化和储存数组数据,然后通过遍历ContentValues的key将对应的值填入对应的表格。

大体的步骤如下:
1、从UI界面获取数据,通过循环填充成数组。

// 得到三个初始化编号的值
    private void SetValuesAtNewTab(View newTab) {

        edit_node_sum = (EditText) newTab.findViewById(R.id.edit_node_sum);
        edit_pole_sum = (EditText) newTab.findViewById(R.id.edit_pole_sum);
        edit_support_sum = (EditText) newTab
                .findViewById(R.id.edit_support_sum);

        mysum_node = Integer.parseInt(edit_node_sum.getText().toString());
        mynm_of_element = Integer.parseInt(edit_pole_sum.getText().toString());
        mynm_of_support_node = Integer.parseInt(edit_support_sum.getText()
                .toString());

        X = new double[mysum_node];
        Y = new double[mysum_node];
        node_F_Disp = new double[2 * mysum_node];
        node_ID = new int[2 * mynm_of_element];
        E = new double[mynm_of_element];

        app.setSum_node(mysum_node);
        app.setNm_of_element(mynm_of_element);
        app.setNm_of_support_node(mynm_of_support_node);

    }

    private void SetValuesAtPointTab(View tabPoint) {

        retrain_matrix = new int[3 * mysum_node];
        et_nodeid = (EditText) tabPoint.findViewById(R.id.et_nodeid);
        et_x = (EditText) tabPoint.findViewById(R.id.et_x_tabpoint);
        et_y = (EditText) tabPoint.findViewById(R.id.et_y_tabpoint);
        et_f_x = (EditText) tabPoint.findViewById(R.id.et_f_x);
        et_f_y = (EditText) tabPoint.findViewById(R.id.et_f_y);
        et_retrain_x = (EditText) tabPoint.findViewById(R.id.et_retrain_x);
        et_retrain_y = (EditText) tabPoint.findViewById(R.id.et_retrain_y);

        int node = Integer.parseInt(et_nodeid.getText().toString());

        if (node > 0) {
            X[node - 1] = Double.parseDouble(et_x.getText().toString());
            Y[node - 1] = Double.parseDouble(et_y.getText().toString());
            node_F_Disp[2 * node - 2] = Double.parseDouble(et_f_x.getText()
                    .toString());
            node_F_Disp[2 * node - 1] = Double.parseDouble(et_f_y.getText()
                    .toString());
            retrain_matrix[3 * node - 3] = node;
            retrain_matrix[3 * node - 2] = Integer.parseInt(et_retrain_x
                    .getText().toString());
            retrain_matrix[3 * node - 1] = Integer.parseInt(et_retrain_y
                    .getText().toString());
        } else {
            Toast.makeText(this, "ID最小为0", Toast.LENGTH_SHORT).show();
        }

        // 传值
        app.setX(X);
        app.setY(Y);
        app.setNode_F_Disp(node_F_Disp);
        app.setRetrain_matrix(retrain_matrix);
        app.setBoundDisp_NodeForce(BoundDisp_NodeForce);

    }

    private void SetValuesAtBarTab(View tabBar) {

        et_id_pole = (EditText) tabBar.findViewById(R.id.et_id_pole);
        et_node_left = (EditText) tabBar.findViewById(R.id.et_node_left);
        et_node_right = (EditText) tabBar.findViewById(R.id.et_node_right);
        et_prop = (EditText) tabBar.findViewById(R.id.et_prop);
        int id_pole = Integer.parseInt(et_id_pole.getText().toString());
        node_ID[2 * id_pole - 2] = Integer.parseInt(et_node_left.getText()
                .toString());
        node_ID[2 * id_pole - 1] = Integer.parseInt(et_node_right.getText()
                .toString());
        E[id_pole - 1] = Double.parseDouble(et_prop.getText().toString());

        app.setNode_ID(node_ID);
        app.setE(E);
    }

2、通过定义自定义Application的变量,传递数组数据。
不用参数进行传递的形式的原因是数据量太多,意味着核心算法主类的构造方法的参数和成员变量非常多。
不用static修饰变量使数据持久化的原因,不适合Android的体系,在UI界面中每个Activity都有生命周期,意味着类被加载后可能会被终止,意味着static可能为null。

package com.work.gongchenglion.app;

import android.app.Application;
import android.content.ContentValues;
import android.util.DisplayMetrics;

public class AppContext extends Application {

    private AppConfig config;
    private int screenWidth;
    private int screenHeight;

    // 三个编号变量
    int sum_node, nm_of_support_node, nm_of_element;

    /*
     * 点界面的成员变量,被节点编号唯一确定
     */
    // 节点编号,在Activity界面辅助生成数组
    // 点的X,y坐标值
    double[] X;
    double[] Y;
    // 受力的XY的坐标值,在一个数组里
    double[] node_F_Disp;
    // 约束的数组三个值为一组,第一个是节点编号,第二三个是是否XY约束。
    int[] retrain_matrix;
    // 约束位移
    double[] BoundDisp_NodeForce;

    /*
     * 杆界面的成员变量
     */
    // 左右节点编号
    int[] node_ID = { 1, 3, 2, 3, 4, 3, 2, 4 };
    // 杆单元的截面积
    double[] E = { 1, 1, 1, 1 };

    ContentValues coreValues;

    @Override
    public void onCreate() {
        super.onCreate();

        // //初始化Toast工具
        // ToastUtil.init(this);

        // 初始化应用配置对象
        config = AppConfig.init(this);
        // 初始化屏幕长宽度
        getScreenSize();
        // 得到三个初始化编号的值

    }

//  /*
//   * @ 重置按钮或者返回按钮时调用
//   */
//  public void stop() {
//
//      node_ID = null;
//      X = null;
//      Y = null;
//      node_F_Disp = null;
//      E = null;
//  }

    private void getScreenSize() {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        screenWidth = metrics.widthPixels;
        screenHeight = metrics.heightPixels;
    }

    /**
     * 获取屏幕的宽度
     *
     * @return screenWidth
     */
    public int getScreenWidth() {
        return screenWidth;
    }

    /**
     * 获取屏幕的高度
     *
     * @return screenHeight
     */
    public int getScreenHeight() {
        return screenHeight;
    }

    /**
     * 获取配置对象
     *
     * @return config
     */
    public AppConfig getConfig() {
        return config;
    }

    public int getSum_node() {
        return sum_node;
    }

    public void setSum_node(int sum_node) {
        this.sum_node = sum_node;
    }

    public int getNm_of_support_node() {
        return nm_of_support_node;
    }

    public void setNm_of_support_node(int nm_of_support_node) {
        this.nm_of_support_node = nm_of_support_node;
    }

    public int getNm_of_element() {
        return nm_of_element;
    }

    public void setNm_of_element(int nm_of_element) {
        this.nm_of_element = nm_of_element;
    }

    public double[] getX() {
        return X;
    }

    public void setX(double[] x) {
        X = x;
    }

    public double[] getY() {
        return Y;
    }

    public void setY(double[] y) {
        Y = y;
    }

    public double[] getNode_F_Disp() {
        return node_F_Disp;
    }

    public void setNode_F_Disp(double[] node_F_Disp) {
        this.node_F_Disp = node_F_Disp;
    }

    public int[] getRetrain_matrix() {
        return retrain_matrix;
    }

    public void setRetrain_matrix(int[] retrain_matrix) {
        this.retrain_matrix = retrain_matrix;
    }

    public double[] getBoundDisp_NodeForce() {
        return BoundDisp_NodeForce;
    }

    public void setBoundDisp_NodeForce(double[] boundDisp_NodeForce) {
        BoundDisp_NodeForce = boundDisp_NodeForce;
    }

    public int[] getNode_ID() {
        return node_ID;
    }

    public void setNode_ID(int[] node_ID) {
        this.node_ID = node_ID;
    }

    public double[] getE() {
        return E;
    }

    public void setE(double[] e) {
        E = e;
    }

    public ContentValues getCoreValues() {
        return coreValues;
    }

    public void setCoreValues(ContentValues coreValues) {
        this.coreValues = coreValues;
    }

}

3、经过算法得到结果的数组,为每一个数组的每一个值赋予一个key,将所有数据封装在一个ContentValues实例中,然后作为返回值返回。

// 处理和封装结果值
        // 点的结果
        for (int i = 0; i < sum_node; i++) {
            resultValues.put("node_Disp_X" + i + 1, node_Disp[2 * i]);
            resultValues.put("node_Disp_Y" + i + 1, node_Disp[2 * i + 1]);
            resultValues.put("node_FORCE_X" + i + 1, node_FORCE[2 * i]);
            resultValues.put("node_FORCE_Y" + i + 1, node_FORCE[2 * i + 1]);
        }
        // 杆的结果
        for (int i = 0; i < nm_of_element; i++) {
            resultValues.put("element_FORCE" + i + 1, element_FORCE[i]);
        }

        return resultValues;
    }

4、将ContentValues的值通过循环,依次提出,并填入到自定义的组件中。

/*
     * *************************************************************** /*
     * 
     * 添加数据核心步骤方法
     */
    // 点表总方法
    private void addDataNode() {

        ViewGroup root_node = (ViewGroup) findViewById(R.id.linearlayout_head_node);

        int[] arrNodeHeadWidth = null;// 首行Cell的Width集合

        HashMap<String, HeadItemCell> headMap = new HashMap<String, HeadItemCell>();// 包含整个首行信息的泛型
        // 增加首行的內容
        this.cellAddHead(headMap, "节点ID");// 得到新的headMap
        this.cellAddHead(headMap, "节点力偏移X");
        this.cellAddHead(headMap, "节点力偏移Y");
        this.cellAddHead(headMap, "节点力X");
        this.cellAddHead(headMap, "节点力Y");
        arrNodeHeadWidth = this.addHead(headMap, arrNodeHeadWidth, root_node);
        // 添加主体内容
        this.addNodeContent();
        // 设置展示的样式
        adapter_node = new CustomeTableViewAdapter(this, lists, lv_node, false,
                arrNodeHeadWidth);

        // 调用父类的方法,监听内容是否改变
        adapter_node.notifyDataSetChanged();

    }

    // 杆表总方法
    private void addPoleNode() {

        ViewGroup root_pole = (ViewGroup) findViewById(R.id.linearlayout_head_pole);

        int[] arrPoleHeadWidth = null;// 首行Cell的Width集合

        HashMap<String, HeadItemCell> headMap = new HashMap<String, HeadItemCell>();// 包含整个首行信息的泛型
        // 增加首行的內容
        this.cellAddHead(headMap, "杆ID");// 得到新的headMap
        this.cellAddHead(headMap, "杆力");

        arrPoleHeadWidth = this.addHead(headMap, arrPoleHeadWidth, root_pole);
        // 添加主体内容
        this.addPoleContent();
        // 设置展示的样式
        adapter_pole = new CustomeTableViewAdapter(this, lists, lv_pole, false,
                arrPoleHeadWidth);
        // 调用父类的方法,监听内容是否改变
        adapter_pole.notifyDataSetChanged();
    }

    /*
     * 增加首行的內容
     */

    // 添加首行的单个Cell
    private void cellAddHead(HashMap<String, HeadItemCell> headMap,
            String headName) {
        HeadItemCell itemCell = new HeadItemCell(headName, 100);
        headMap.put(headMap.size() + "", itemCell);// 加双引号将int强转String
    }

    // 增加整个首行
    private int[] addHead(HashMap<String, HeadItemCell> headMap,
            int[] arrHeadWidth, ViewGroup root) {

        arrHeadWidth = new int[headMap.size()];// 首行Cell的Width集合
        int width = 0;
        for (int i = 0; i < headMap.size(); i++) {
            HeadItemCell itemCell = headMap.get(i + "");// 提出单个Cell
            String name = itemCell.getCellValue();
            width = Dp2Px(this, itemCell.getWidth());

            setHeadName(name, width, root);// 设置文字的存储方式

            arrHeadWidth[i] = width;
            if (i != headMap.size() - 1) {// 添加竖线
                this.addVLine(root);
            }
        }
        return arrHeadWidth;
    }

    // 向headView添加TextView
    private void setHeadName(String name, int width, ViewGroup root) {
        TextView headView = (TextView) inflater.inflate(
                R.layout.atom_head_text_view, root,false);
        if (headView != null) {
            String viewName = "<b>" + name + "</b>";
            headView.setText(Html.fromHtml(viewName));
            headView.setWidth(width);
            root.addView(headView);
        }
    }

    /*
     * 添加主体内容
     */
    // 添加点主体数据
    private void addNodeContent() {

        int sum_node = app.getSum_node();

        for (int i = 0; i < sum_node; i++) {
            HashMap<String, Object> rowMap = new HashMap<String, Object>();// 一行的数据
            lists.add(rowMap);

            this.addRows(rowMap, i + "", CellTypeEnum.LABEL);
            this.addRows(rowMap, resultValues.get("node_Disp_X" + i + 1)
                    .toString(), CellTypeEnum.LABEL);
            this.addRows(rowMap, resultValues.get("node_Disp_Y" + i + 1)
                    .toString(), CellTypeEnum.LABEL);
            this.addRows(rowMap, resultValues.get("node_FORCE_X" + i + 1)
                    .toString(), CellTypeEnum.LABEL);
            this.addRows(rowMap, resultValues.get("node_FORCE_Y" + i + 1)
                    .toString(), CellTypeEnum.LABEL);
        }
    }

    // 添加杆主体数据
    private void addPoleContent() {
        int nm_of_element = app.getNm_of_element();

        for (int i = 0; i < nm_of_element; i++) {
            HashMap<String, Object> rowMap = new HashMap<String, Object>();// 一行的数据
            lists.add(rowMap);
            this.addRows(rowMap, i + "", CellTypeEnum.LABEL);
            this.addRows(rowMap, resultValues.get("element_FORCE" + i + 1)
                    .toString(), CellTypeEnum.LABEL);
        }
    }

    /*
     * 功能单元
     */
    // 添加一行中单个Cell的数据
    private void addRows(HashMap<String, Object> rowMap, String cellValue,
            CellTypeEnum cellType) {
        ItemCell itemCell = new ItemCell(cellValue, cellType);
        rowMap.put(rowMap.size() + "", itemCell);
    }

    private void addVLine(ViewGroup root) {
        LinearLayout v_line = (LinearLayout) getVerticalLine(root);
        v_line.setLayoutParams(new LinearLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
        root.addView(v_line);
    }

    private View getVerticalLine(ViewGroup root) {
        return inflater.inflate(R.layout.atom_line_v_view, root,false);
    }

数据处理的过程中,70%的时间在找bug,80%的bug是数组溢出和空值。数组溢出是大多由于length和index函数计算错误,空值大多数是由于逻辑错误(比如构造函数和成员变量初始化),初始化和赋值顺序颠倒等。不过熟练的掌握debug,可以最快找到并解决。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值