【问题标题】:How to create a Data Grid for Android?如何为 Android 创建数据网格?
【发布时间】:2015-07-12 21:57:24
【问题描述】:

我的应用必须在二维网格中显示数据。网格可以有多个行和列(10 x 10 或 100 x 44)。并且网格必须显示列名和行名。

基本上,我想要 Windows 窗体和 WPF 中的 DataGridView 之类的东西。

请提供帮助。谢谢。

【问题讨论】:

  • 这是一个原生的Android数据网格组件,它与Windows Forms DataGridView非常相似,并提供了它的大部分功能。 androidjetpack.com/Home/AndroidDataGrid

标签: android datagrid


【解决方案1】:

您应该使用TableLayout,动态添加TableRow 以及与您希望添加的列相对应的TextViews。为了使网格看起来你应该添加一个可绘制的形状作为每个TextView 的背景可绘制对象,并带有白线以便拥有单元格。

示例: 在 layout.xml 中:

...
<TableLayout id="grid" *other properties*/>
...

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

class Data {
  ArrayList<Row> rows;
  ArrayList<Column> column;
  //or some other properties you might need
}

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

【讨论】:

    【解决方案2】:

    如果您有一些确定的行数和列数并且这个数字不大,那么您可能对TableLayout 没问题 - 只需在循环中创建它或通过一些延迟的部分创建它。 否则(如果需要的行数/列数不清楚,或者您需要一些特定的滚动)将需要自定义控件。

    【讨论】:

    • 是的,在使用 TableLayout 时。如果您要为此创建自己的视图,那么应该可以为它创建自己的适配器。
    【解决方案3】:

    我建议创建一个列表视图并在其中添加数据。

    这是回答另一个问题的一个例子 Populating a table with an array of data

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      相关资源
      最近更新 更多