// suppress this error message to be able to use spaces in higher api levels
  @SuppressLint("NewApi")
  public void refresh() {

    if (mAdapter == null) {
      mAdapter = new StackAdapter(mContext, mStacks, mSwipeable);
      if (mListView != null) {
        mListView.setAdapter(mAdapter);
      } else if (mTableLayout != null) {
        TableRow tr = null;
        for (int i = 0; i < mAdapter.getCount(); i += mColumnNumber) {
          // add a new table row with the current context
          tr = new TableRow(mTableLayout.getContext());
          tr.setOrientation(TableRow.HORIZONTAL);
          tr.setLayoutParams(
              new TableRow.LayoutParams(
                  TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
          // add as many cards as the number of columns indicates per row
          for (int j = 0; j < mColumnNumber; j++) {
            if (i + j < mAdapter.getCount()) {
              View card = mAdapter.getView(i + j, null, tr);
              if (card.getLayoutParams() != null) {
                card.setLayoutParams(
                    new TableRow.LayoutParams(
                        card.getLayoutParams().width, card.getLayoutParams().height, 1f));
              } else {
                card.setLayoutParams(
                    new TableRow.LayoutParams(
                        TableRow.LayoutParams.MATCH_PARENT,
                        TableRow.LayoutParams.WRAP_CONTENT,
                        1f));
              }
              tr.addView(card);
            }
          }
          mTableLayout.addView(tr);
        }
        if (tr != null) {
          // fill the empty space with spacers
          for (int j = mAdapter.getCount() % mColumnNumber; j > 0; j--) {
            View space = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
              space = new Space(tr.getContext());
            } else {
              space = new View(tr.getContext());
            }
            space.setLayoutParams(
                new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
            tr.addView(space);
          }
        }
      }
    } else {
      mAdapter.setSwipeable(mSwipeable); // in case swipeable changed;
      mAdapter.setItems(mStacks);
    }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 声明二维数组
    String[][] strs = new String[5][3];
    String str = null;
    // 创建Tablelayout
    tableLayout = new TableLayout(this);
    tableLayout.setOrientation(TableLayout.VERTICAL);
    // 设定tablelayout宽高属性
    TableLayout.LayoutParams lp =
        new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    // 绑定参数
    tableLayout.setLayoutParams(lp);

    for (int i = 1; i <= strs.length; i++) {
      // 创建tablerow
      TableRow tableRow = new TableRow(this);
      tableRow.setOrientation(TableRow.HORIZONTAL);
      tableRow.setBackgroundColor(Color.GREEN);
      // 设定tablerow的宽高
      TableRow.LayoutParams trLp =
          new TableRow.LayoutParams(
              TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
      tableRow.setLayoutParams(trLp);

      for (int j = 1; j <= strs[0].length; j++) {
        str = "第" + i + "行," + "第" + j + "列";

        TextView mtView = new TextView(this);

        // 添加textview文本信息
        mtView.setText(str);

        if (j == 1) {
          mtView.setBackgroundColor(Color.RED);
        } else if (j == 2) {
          mtView.setBackgroundColor(Color.GREEN);
        } else if (j == 3) {
          mtView.setBackgroundColor(Color.BLUE);
        }
        // 设定textview宽高以及权重

        TableRow.LayoutParams tvLp =
            new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1);
        // 给textview设定参数
        mtView.setLayoutParams(tvLp);

        // 将textview添加至tablerwo中
        tableRow.addView(mtView);

        Toast.makeText(this, str, 1).show();
      }
      tableLayout.addView(tableRow);
    }
    setContentView(tableLayout);
  }
  /**
   * Creates the actual minesweeper board as well as all Boxes. Sets up certain boxes as mines.
   *
   * @param frame2 tablerow to hold board
   * @param x number of rows
   * @param y number of columns
   * @param mines number of mines
   * @param context context of activity
   */
  public void createBoard(ViewGroup frame2, int x, int y, int mines, MainActivity context) {
    TableLayout frame = (TableLayout) frame2;
    this.context = context;
    this.context.minesRemaining.setText(Integer.toString(mines));
    this.x = x;
    this.y = y;
    this.mines = mines;
    this.boxesFilled = 0;
    this.isGoing = false;
    this.isPaused = false;
    int minesCreated = 0;
    this.board = new Box[x][y];

    for (int i = 0; i < this.x; ++i) {
      TableRow row = new TableRow(context);
      int height =
          (int)
              TypedValue.applyDimension(
                  TypedValue.COMPLEX_UNIT_DIP,
                  (float) 48.0,
                  context.getResources().getDisplayMetrics());
      TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, height);
      params.gravity = Gravity.CENTER;
      params.setMargins(0, 0, 0, 0);
      row.setLayoutParams(params);
      row.setOrientation(0);
      int padding =
          (int)
              TypedValue.applyDimension(
                  TypedValue.COMPLEX_UNIT_DIP,
                  (float) -2,
                  context.getResources().getDisplayMetrics());
      row.setPadding(padding, padding, padding, padding);

      for (int j = 0; j < this.y; ++j) {
        this.board[i][j] = new Box(this, context);
        final int localI = i;
        final int localJ = j;
        final MainActivity newcontext = context;
        this.board[i][j].setOnClickListener(
            new OnClickListener() {
              public void onClick(View v) {
                final int result;
                if (newcontext.flagMode == 1) {
                  result = flagIt(localI, localJ);
                } else {
                  result = hitIt(localI, localJ);
                }
                getResult(result);
              }
            });
        row.addView(this.board[i][j]);
      }
      frame.addView(row);
    }

    // use a while loop for more randomness
    while (minesCreated < this.mines) {
      Random r = new Random();
      int i = r.nextInt(this.x);
      int j = r.nextInt(this.y);
      double rando =
          (((this.mines - minesCreated) / ((this.x * this.y) - ((i * this.y) + j) * 1.0)) * 10);
      int surroundingMines = this.getSurrounding(i, j);
      int temp = r.nextInt(10 - (surroundingMines / 2));
      if (rando >= temp && this.board[i][j].isMine() == 0) {
        this.board[i][j].makeMine();
        this.addSurrounding(i, j);
        minesCreated++;
      }
    }
    /*for (int i = 0; i < this.x; ++i) {
    	for (int j = 0; j < this.y; ++j) {
    		Random r = new Random();
    		double rando =  (((this.mines-minesCreated) / ((this.x*this.y)-((i * this.y) + j)*1.0)) * 10);
    		int surroundingMines = this.getSurrounding(i, j);
    		int temp = r.nextInt(10-surroundingMines);
    		if (rando >= temp && minesCreated < this.mines) {
    			this.board[i][j].makeMine();
    			this.addSurrounding(i, j);
    			minesCreated++;
    		}
    	}
    }*/

    // zoom in and out code
    final Minesweeper ms2 = this;
    ImageView zoomOutButton = (ImageView) context.findViewById(R.id.zoomout);
    zoomOutButton.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            ms2.resizeMe(1);
          }
        });

    ImageView zoomInButton = (ImageView) context.findViewById(R.id.zoomin);
    zoomInButton.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            ms2.resizeMe(0);
          }
        });

    context.flagButton.setImageDrawable(context.getResources().getDrawable(R.drawable.mine));
    context.flagMode = 0;
  }