Saturday, November 26, 2011

DataGrid Part 2 - DataTable for DataGrid

If you have not read the previous posts, you might want to start from the first post.

Before we going deeper in each component of DataGrid, we have to explore data table before others. In previous post, we have following code
DataGrid.setNoDataText("No data available");
DataGrid.addColumnStyle(new cls_datagrid.column("Header 1", "header_5", 50));
DataGrid.addColumnStyle(new cls_datagrid.column("Header 2", "header_4", 80));
DataGrid.addColumnStyle(new cls_datagrid.column("Header 3", "header_3", 130));
DataGrid.addColumnStyle(new cls_datagrid.column("Header 4", "header_2", 120));
DataGrid.addColumnStyle(new cls_datagrid.column("Header 5", "header_1", 80));
DataGrid.addColumnStyle(new cls_datagrid.column("Header 6", "header_0", 140));
DataGrid.setDataSource(DataSource);
DataGrid.refresh();
DataSource is data table we use. Firstly, we define the column for DataTable.
DataSource = new cor_db.cls_datatable(); 
DataSource.addAllColumns(new String[]{"header_0","header_1","header_2","header_3","header_4","header_5","header_6"});
Note that the columns name above (i.e. header_0, header_1...) match with the one in column style above. Next, we populate the data as from cursor which data comes from database.
cor_db.cls_datatable.cls_datarow dr;
DataSource.clear(); 
if(csr.moveToFirst()){ 
 do{ 
  dr = DataSource.newRow();
  dr.set("header_0", csr.getString(csr.getColumnIndex("field_1")));
  dr.set("header_1", csr.getString(csr.getColumnIndex("field_2")));
  dr.set("header_2", csr.getString(csr.getColumnIndex("field_3")));
  dr.set("header_3", csr.getString(csr.getColumnIndex("field_4")));
  dr.set("header_4", csr.getString(csr.getColumnIndex("field_5")));
  dr.set("header_5", csr.getString(csr.getColumnIndex("field_6")));
  dr.set("header_6", csr.getString(csr.getColumnIndex("field_7")));
  DataSource.add(dr);
 } while(csr.moveToNext());
 csr.close();
} 
In snippet code above, we have DataRow dr, if you familiar with DataTable object in .Net, this DataRow should be not a new thing to you. I borrowed the concept from .Net. set() takes 2 parameters, first one is column name and second one is the value. So, we have the DataSource ready for the DataGrid. DataGrid currently only take type DataTable as data source for the grid.

Now, take a closer look at DataTable class. Basically, DataTable has array list of Sting for column and DataRow for row. DataRow is resides in DataTable. In DataRow, an ArrayList of String is used to store the value of each column value.
public class cls_datatable{

 private ArrayList<String> column = new ArrayList<String>();
 private ArrayList<cls_datarow> row = new ArrayList<cls_datarow>();
 private int indexCounter = 0;

 public class cls_datarow{
  
  private ArrayList<String> columnValues;
  private int id;
  
  public cls_datarow(int id)
  {
   this.id = id;
   columnValues = new ArrayList<String>();
   for(int i = 0; i < column.size() ; i++ ) columnValues.add(null);
  }
 
  public cls_datarow()
  {
   columnValues = new ArrayList<String>();
   for(int i = 0; i < column.size() ; i++ ) columnValues.add(null);
  }
  
  public int getID()
  {
   return id;
  }
  
  public String get(String columnName)
  {
   return columnValues.get(column.indexOf(columnName));
  }
  
  public String get(int columnIndex)
  {
   return columnValues.get(columnIndex);
  }
  
  public void set(String columnName, String value)
  {
   columnValues.set(column.indexOf(columnName), value);
  }
  
  public void set(int columnIndex, String value)
  {
   columnValues.set(columnIndex, value);
  }
  
  public int getColumnSize()
  {
   return column.size();
  }
 }
 
 public void addColumns(String[] columns)
 {
     for (String col : columns) {
      column.add(col);
     }
 }
 
 public int getColumnSize()
 {
  return column.size();
 }
 
 public int getColumnIndex(String columnName)
 {
  return column.indexOf(columnName);
 }
 
 public String getColumnName(int intIndex)
 {
  return column.get(intIndex);
 }
 
 public int getRowSize()
 {
  return row.size();
 }
 
 public cls_datarow getRow(int rowIndex)
 {
  return row.get(rowIndex); 
 }
 
 public void remove(int rowIndex)
 {
  row.remove(rowIndex); 
  refreshIDnIndex();
 }
 
 public cls_datarow newRow()
 {
  cls_datarow dr = new cls_datarow(indexCounter);
  indexCounter++;
  return dr;
 }
 
 public void clear()
 {
  row.clear();
 }
}
As for now, we have following code in Activity.
//get data from database
Cursor csr = getDataFromDB(); 

//create DataTable object
cor_db.cls_datatable DataSource = new cor_db.cls_datatable(); 

//define column
DataSource.addColumn(new String[]{"header_0","header_1","header_2","header_3","header_4","header_5"}); 

//create DataRow
cor_db.cls_datatable.cls_datarow dr;

//populate data from cursor into DataSource
if(csr.moveToFirst()){ 
 do{ 
         dr = dtCust.newRow();
  dr.set("header_0", csr.getString(csr.getColumnIndex("field_1")));
  dr.set("header_1", csr.getString(csr.getColumnIndex("field_2")));
  dr.set("header_2", csr.getString(csr.getColumnIndex("field_3")));
  dr.set("header_3", csr.getString(csr.getColumnIndex("field_4")));
  dr.set("header_4", csr.getString(csr.getColumnIndex("field_5")));
  dr.set("header_5", csr.getString(csr.getColumnIndex("field_6")));
  DataSource.add(dr);
 } while(csr.moveToNext());
 csr.close();
}

//initialize DataGrid
DataGrid = (cls_datagrid) findViewById(R.id.grid);

//define column style, bond each DataGrid column by DataTable column
DataGrid.addColumnStyle(new cls_datagrid.column("Header 1", "header_5", 50));
DataGrid.addColumnStyle(new cls_datagrid.column("Header 2", "header_4", 80));
DataGrid.addColumnStyle(new cls_datagrid.column("Header 3", "header_3", 130));
DataGrid.addColumnStyle(new cls_datagrid.column("Header 4", "header_2", 120));
DataGrid.addColumnStyle(new cls_datagrid.column("Header 5", "header_1", 80));
DataGrid.addColumnStyle(new cls_datagrid.column("Header 6", "header_0", 140));

//set the DataTable as source
DataGrid.setDataSource(DataSource);

//generate the DataGrid 
DataGrid.refresh();
Next, DataGrid Part 3 - DataGrid.column

4 comments:

  1. Thank you Very Much!!!!

    ReplyDelete
  2. Hi HJ

    Can u please provide the complete code as a zip file on my emailid: shruti@essito.com.

    Thank you very much in advance!!

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. how to populate it from the result set from a stored procedure?

    ReplyDelete