In the layout file, we have
cor_customctrl.data_grid.cls_datagrid is the custom datagrid we are using. We have to set the height and the width to the value we want.In the activity file, as usual, we need to initialize
DataGrid = (cls_datagrid) findViewById(R.id.grid);We then have to construct the DataGrid as below.
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();
Done! That's all we need to do to set up a datagrid.- setNoDataText() is to set the default text to display if there is empty data.
 - addColumnStyle() is to create the datagrid column. The first parameter is the column name to display, second parameter is the data source's field name which data to be bond to. The last parameter is the width of the column.
 - setDataSource() is to set the data source. Data source is refers to a table, cls_datatable which concept is similar to .Net's DataTable.
 - refresh()is the final step to create and display the DataGrid.
 
In additional to features above, if you do need event for DataGrid, there are common events such as click and long click could be used.
DataGrid.setLvOnItemClickListener(new AdapterView.OnItemClickListener(){
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    //your code here.
  } 
});
DataGrid.setLvOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
  @Override
  public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    //your code here.
  }
});
That's how to set up DataGrid. The convention for naming class (i.e.cls_x perhaps isn't appropriate, the sample was written quite some time ago. I apologize if you find it uncomfortable. I couldn't find any similar control in the past and hence I write my own and now share it out here.
Next DataGrid Part 2 - DataTable for DataGrid
I'm new in this... how can I get the values from arg1 in onItemClick?... please help me!!!
ReplyDeletereportGrid.setLvOnItemClickListener(new AdapterView.OnItemClickListener(){
ReplyDelete@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getActivity(), "Click!" + String.valueOf(arg2)+"-"+String.valueOf(arg3), Toast.LENGTH_LONG).show();
}
});