Android Tutorial Grid Sample

Hey all,

This grid is like an open source project. It is completely written in a seperate B4A module and allows paging, 'custom' column types, horizontal and vertical scrolling, summation and basic filtering. It uses Nine Patch Images for the backgrounds of the headers, records and footers. The developer can provide the eventnames he/she wants to get triggered in the activity.

Not everything is tested but it is easy to change or adapt to your needs.

So, if your interested in this, just try the sample and be creative in here :).

Update: I've added a lot of comments to the source code and added the possibility the catch the header click event in the activity. Did some cleanup and fixed a spacing error.

Update2: Added basic ability for number formatting. To use it, you have to set the property 'NumberIntegersMin' higher than 0 by the column. 'NumberFractions' is for decimal places and 'NumberGrouping' is for grouping per 3.

Update 3: Added a DB Sample, changed some minor issues with column widths and backpanels in the DB Sample (grid code module).

Update 4: Custom Color Rules added by request on the B4A chat. With simple color rules you can now change the textcolor of the fields by a value1 and value2. You can use this for numbers or strings. See the 'ProAppGridSample' for an example.

Update 5: ProAppGridAdvSample. Added Row and Field selection (needs some activity code). Also added the possibility to change the value of a field without redrawing the whole grid. I have only implemented the Label function and it's taking autoheight into account.

Update 6: Added sorting (QuickSort) functionality and custom typefaces per column.

Update 7: Added MinHeightField to the TColumn definition. This way you can specify the height of a column by the value of another column. I updated the ProAppGridAdvSample with this and I've also added the sorting by clicking on the header in this sample.

-------------------------------------------------------------
Some screenshots of what is possible with this grid:
proappgridadvsample01.jpg
proappdbexplorer01.jpg


proappdbexplorer02.jpg


proappdbexplorer04.jpg


Greetings,
Sigurd
 

Attachments

  • ProAppGridDBSample.zip
    51.7 KB · Views: 1,651
  • ProAppGridSample.zip
    35.5 KB · Views: 1,407
  • ProAppGridAdvSample.zip
    37.9 KB · Views: 1,292
Last edited:

COBRASoft

Active Member
Licensed User
Longtime User
Selection

About selection... like said before, the grid is able to use custom controls like radiobuttons and checkbuttons. Not only is this easier for the developer, but also better for the user to see what is selected and what not.

It is not possible to fully implement 'row' selection in the grid module alone because the click events have to be dealt with in the activity. This would require a Java library I guess and this is out of the scope of this example.
 

COBRASoft

Active Member
Licensed User
Longtime User
Advanced sample

Hi,

Since a lot of people seem to have problems implementing/extending my grid sample, I have added an advanced sample with row/field selection in about 2 hours :D.

I also added the functionality to change a field without redrawing the whole grid (only working for labels, but can easily be extended).

The advanced sample has 2 things added. Click on a number field and it will change it's value (see activity code grdSample_Click) and update according to the color rules. It will also update the footer. Click on the ID column to use row/field selection (use the radiobuttons on the top to indicate selectionmode). I've added some helper functions in the Grid module for selection/deselection and DataRowIndex <-> VisibleRowIndex conversions.

1 more thing: a field (TField) is a part of a row (TRow). Look into the Grid module for more info.

C&C welcome...

Greetings,
Sigurd
 

COBRASoft

Active Member
Licensed User
Longtime User
Added custom height for a column based on the value of another column. Implemented the sorting by columnheader in the advanced sample at the same time.
 

junaidahmed

Well-Known Member
Licensed User
Longtime User
Sorry,I need example code for selection row (highlight row)for grid,it would be most appreciate for you.
 

jfranz

Member
Licensed User
Longtime User
Issue running any of the examples

I am trying to run your examples and am getting the following error.

Program pause on line 583
idDefault = r.GetStaticField(package & ".R$drawable", DefaultImage)

Looks like its trying to reference a static inside the package, but since I'm not familiar with Android and relection I'm not sure what is going on.

I simply downloaded and installed the package and tried to run it on B4A 1.90.

What am I missing/doing wrong?
 

jfranz

Member
Licensed User
Longtime User
Never mind. I figured what I was doing wrong. Things get buried pretty quickly and missed the thing about nine patch images. I also needed to clean the project again after reinstalling the missing png files.
 

Sytek

Active Member
Licensed User
Longtime User
I'm Sorry I cannot run this one.

It throws me this pacckage error

Compiling code. Error
Error compiling program.
Error description: Unknown member: getstaticfield
Occurred on line: 582
package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
Word: getstaticfield

Best Regards
Sytek
 

Sytek

Active Member
Licensed User
Longtime User
Please Forgive me; I'm Really Really new on this, would you please point me in the right direction?

Thank's in advance
Regards
Sytek
 

Sytek

Active Member
Licensed User
Longtime User
Hi Erel, Thanks for the reply
I'm just running the project provided by CobraSOFT
http://www.b4x.com/forum/attachments/basic4android-getting-started-tutorials/11376d1334360254-grid-sample-proappgridsample.zip

..Here's the r
Sub SetNinePatchButton(Btn As Button, DefaultImage As String, PressedImage As String)
Dim r As Reflector
Dim package As String
Dim idDefault, idPressed As Int
package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
idDefault = r.GetStaticField(package & ".R$drawable", DefaultImage)
idPressed = r.GetStaticField(package & ".R$drawable", PressedImage)
r.Target = r.GetContext
r.Target = r.RunMethod("getResources")
Dim sd As StateListDrawable
sd.Initialize
sd.AddState(sd.State_Pressed, r.RunMethod2("getDrawable", idPressed, "java.lang.int"))
sd.AddCatchAllState( r.RunMethod2("getDrawable", idDefault, "java.lang.int"))
Btn.Background = sd
End Sub


Best Regards
Sytek
 

Sytek

Active Member
Licensed User
Longtime User
Thank's for your quick reply Erel, That was the problem.
Weird 'cause i got version 1.90

I've downloaded the 2.0 reflection version and that solves my problem

Best Regards
Sytek
 

EduardoElias

Well-Known Member
Licensed User
Longtime User
I am having a problem with the code below from the grid class:

Sub GetDefaultFontSize() As Int
Dim lblDefault As Label: lblDefault.Initialize("")
Return lblDefault.TextSize
End Sub

Compiler says that wants an Activity Object. I am calling this class from another class, that if i understand correctly is out of the activity context.

Is there a way to reproduce the same behavior without needing the activity context?

EDIT: Actually going further, I have found that the Grid.Initialize also requires a Activity context. What I am trying to do is to somehow encapsulate the grid creation in a class that also knows how to handle the data, so in the activity I only define the Grid variable globally and initialize the Grid calling a method from the class.


Sub Globals
Dim pnPedido As Panel
Dim grdPedido As TGrid

Sub Activity_Create
Dim FPedidos As Pedidos
FPedidos.Initialize
FPedidos.InitializeGrid(grdPedidos, pnPedidos)


what i am trying to achieve is to create apps that use the same classes for the data. Each app will have different ways to visualize data, so the activity is only the specific code for the overall UI, the specific code for Grid remains the almost the same.
How can I solve this Acitivity conext in this case?

And another question, i see that each time i try to use a class from another project, b4a copy the file to the project. How can I share classes thru projects? like a repository (is it the function of the library? Could the library fix the problem above?)

EDIT: I have found that this post gives the workaround
http://www.b4x.com/forum/bugs-wishl...-no-activity-context-required.html#post111175
 
Last edited:

junaidahmed

Well-Known Member
Licensed User
Longtime User
This Grid Sample is fine when compare to other grid example,but we are facing problem for selecting row (Highlight row), how to remove the color of previously selected row.pls advise ASAP.
 

COBRASoft

Active Member
Licensed User
Longtime User
Hi,

Use the latest ProApp Grid Sample. In line 47, change the GRADIOBUTTON to GCHECKBOX, this will show checkboxes instead of radiobuttons. For the sample to work completely, you also have to change line 177, RadioButton to CheckBox. The sample will now show a checkbox instead of a radiobutton.

Greetings,
Sigurd
 
Top