如何为Android创建数据网格?

jas*_*as7 4 datagrid android

我的应用程序必须在2D网格中显示数据.网格可以有多个行和列(10乘10或100乘44).并且网格必须显示列名和行名.

基本上我想要一些像Windows Form和WPF的DataGridView.

请提供帮助.谢谢.

10s*_*10s 5

您应该使用a TableLayout,动态添加TableRowTextView您要添加的列对应的s.为了使网格看起来你应该添加一个可绘制的形状作为每个TextView白色线条的背景可绘制,以便有细胞.

示例:在layout.xml中:

...
<TableLayout id="grid" *other properties*/>
...
Run Code Online (Sandbox Code Playgroud)

一个简单的对象具有所有必要属性的数据:

class Data {
  ArrayList<Row> rows;
  ArrayList<Column> column;
  //or some other properties you might need
}
Run Code Online (Sandbox Code Playgroud)

Activity:

private void fillGrid(Data dat,) {
  for(int i=0; i<dat.getRows().size(); i++) {
     TableRow row = new TableRow(this);
     //set row
     for(int j=0; j<dat.getColumns().size(); j++) {
         TextView actualData = new TextView(this);
         //set properties
         row.addView(actualData);
     }
     tableLayout.addView(row);
  }
}
Run Code Online (Sandbox Code Playgroud)