Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Data Grid for Android?

My app has to display data in a 2D grid.The grid can have multiple rows and columns (10 by 10 or 100 by 44). And the grid must display the column and row names.

Basically I want something like the DataGridView from Windows Form and WPF.

Please provide help. Thank you.

like image 667
jas7 Avatar asked Feb 20 '23 19:02

jas7


1 Answers

You should use a TableLayout, dynamically add TableRow with as many TextViews that corresponds to the columns that you wish to add. In order to make the grid look you should add a shape drawable as the background drawable of each TextView with white lines in order to have cells.

Sample: in the layout.xml:

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

a simple object Data that has all the necessary properties:

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

in the 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);
  }
}
like image 92
10s Avatar answered Mar 03 '23 16:03

10s