jas7
jas7

Reputation: 3075

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.

Upvotes: 4

Views: 14848

Answers (3)

Rahul Bagal
Rahul Bagal

Reputation: 230

I would suggest to create a listview and add data in it .

Here is one example in response to another question Populating a table with an array of data

Upvotes: 0

10s
10s

Reputation: 1699

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

Upvotes: 5

sandrstar
sandrstar

Reputation: 12643

If you have some determined number of rows and columns and that number is not big, when you might be ok with TableLayout - just create it in the loop or by portions with some delay. Otherwise (if required number of rows/columns is not clear or you need some specific scrolling) custom control would be needed.

Upvotes: 3

Related Questions