Android Code Snippet Sharing the goodness: Some useful methods

Mashiane

Expert
Licensed User
Longtime User
B4X:
' Helper function to get the icon drawable
Sub GetDrawable(Name As String) As Object
   Dim r As Reflector
   Dim package As String
   Dim id As Int
   package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
   id = r.GetStaticField(package & ".R$drawable", Name)
   r.Target = r.GetContext
   r.Target = r.RunMethod("getResources")
   Return r.RunMethod2("getDrawable", id, "java.lang.int")
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
In my past life of VB6, just like you have noted, I also learned for each project to just only use the code applicable to that app. What I usually did was to have code modules with code that I knew I will use sometime in the future and compiled this as a dll. As an example, using code modules with code that does not even apply to an app in the store will give permissions to app for functionality that is not even used by the app and thus dangerous.

I think someone suggested a code snippet manager for B4x, it will indeed come in handly because you will just copy and paste what you need. For now, B4x highlights unused subs and variables, one can just comment these out for the final app. Thanks
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: Convert rgb to hex string
'tag: rgb, hex, conversion
Sub Rgb2Hex(r As Int, g As Int, b As Int) As String
    Dim intC As Int
    Dim bc As ByteConverter
    intC = Colors.RGB(r,g,b)
    Return Bit.ToHexString(intC)
End Sub

'Description: convert hex string to rgb
'Tag: rbg, hex, conversion
Sub Hex2RGB(hex As String) As Map
    Dim m As Map
    m.Initialize
    Dim bc As ByteConverter
    Dim r,g,b As Int
    ' #E3E2E1
    'Log(hex.SubString2(1,3))
    r = Bit.ParseInt(hex.SubString2(1,3), 16)
    g = Bit.ParseInt(hex.SubString2(3,5), 16)
    b = Bit.ParseInt(hex.SubString2(5,7), 16)
    m.Put("r",r)
    m.put("g",g)
    m.Put("b", b)
    Return m
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
'Description: Create indexes on an SQLite table
' Tag: sqlite, index, create
B4X:
' update an existing table and add columns
Sub AddIndexes(SQLite As SQL,TableName As String, Fields As Map)
    Dim i As Int
    For i = 0 To Fields.Size - 1
        Try
        Dim sb As StringBuilder
        Dim field As String
        Dim unique As Boolean
        field = Fields.GetKeyAt(i)
        unique = Fields.GetValueAt(i)
        sb.Initialize
        sb.Append("CREATE ")
        If unique = True Then sb.Append("UNIQUE ")
        sb.Append("INDEX [").Append(TableName).Append(field).Append("]")
        sb.Append(" ON [").Append(TableName).Append("] ([").Append(field).Append("])")
        Log(sb.ToString)
        SQLite.ExecNonQuery(sb.tostring)
        Catch
        End Try
    Next
End Sub

Usage

B4X:
Dim idxs as Map
idxs.initialize
idxs.put("ColumnName", True)
idxs.put("ColumnName1", False)

True above means the index is unique, i.e. no duplicates allowed. You can put this inside DBUtils.
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Fantastic job : thank you !

Just an idea :
put all these codes into an App with some kind of topic search capabilities

I would for sure rate it 5 stars !
 

stanmiller

Active Member
Licensed User
Longtime User
This is a nice collection of how to's...

One caution with any routine manipulating the date format. Because DateTime.DateFormat is global you should save and restore the format on sub entry and exit respectively. Otherwise you could impact the date formatting of the caller.

YearNow with format save/restore
B4X:
'Description: Return the current system year
'Tags: year, date, today
Public Sub YearNow2() As String
    Dim lNow As Long
    Dim dt As String
    Dim saveformat As String
    saveformat = DateTime.DateFormat    ' save current format
    lNow = DateTime.Now
    DateTime.DateFormat = "yyyy"
    dt = DateTime.Date(lNow)
    DateTime.DateFormat = saveformat    ' restore format
    Return dt
End Sub

Nice work over at CodeProject as well!
 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Thanks, brilliant...
 

Mashiane

Expert
Licensed User
Longtime User
I have a table that I want to add records to but first I need to find the next available key, so I decided on a max function where before I add the record, i return the max value of a particular field in the table and then increment that with 1.

The problem with this approach though is that if the table does not have any records, NULL is returned by max and causes a NullException error on the app as my nc = nc + 1 gets a NULL value. After looking around I stumbled accross some function in SQLite called coalesce, so I updated my DBUtils to have this method.

The SQLite coalesce function returns the first non-null expression in the list.

B4X:
public Sub NextCount(SQL As SQL, TableName As String, PrimaryKey As String) As String
    'The SQLite coalesce function returns the first non-null expression in the list.
    Dim ll As List
    ll = ExecuteMemoryTable(SQL, "select coalesce(max(" & PrimaryKey & "),0) as records from " & TableName,Null,1)
    Dim Cols() As String
    Cols = ll.Get(0)
    Return Cols(0)
End Sub

usage

B4X:
Dim nc = DButils.NextCount(SQL,"Components","id")
nc = nc + 1

So if my max(id) is NULL, the function will return 0 which is the default and non NULL value in the list. This solved my problem.

Ta!
 

Mahares

Expert
Licensed User
Longtime User

Mashiane

Expert
Licensed User
Longtime User

Mashiane

Expert
Licensed User
Longtime User
B4X:
'executes a query and returns a result
Sub RunQuery(SQL As SQL, Qry As String)
    Try
        SQL.ExecNonQuery(Qry)
        RanOK = True
    Catch
        RanOK = False
        Log(LastException)
    End Try
End Sub

' update an existing table and add columns
Sub AddIndexes(SQL As SQL,TableName As String, Fields As Map)
    Dim i As Int
    For i = 0 To Fields.Size - 1
        Try
            Dim sb As StringBuilder
            Dim field As String
            Dim unique As Boolean
            field = Fields.GetKeyAt(i)
            unique = Fields.GetValueAt(i)
            sb.Initialize
            sb.Append("CREATE ")
            If unique = True Then sb.Append("UNIQUE ")
            sb.Append("INDEX IF NOT EXISTS [").Append(TableName).Append(field).Append("]")
            sb.Append(" ON [").Append(TableName).Append("] ([").Append(field).Append("])")
            RunQuery(SQL, sb.tostring)
        Catch
            Log("AddIndexes Error: " & LastException)
        End Try
    Next
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
' return a list of the table index names as a map list of name and unique indicator
Sub GetIndexNames(SQL As SQL,TableName As String) As List
    Dim res1 As List
    res1.Initialize
    Try
        Dim cur As ResultSet
        cur = SQL.ExecQuery("PRAGMA INDEX_LIST ('" & TableName & "')")
        Dim table As List
        table.Initialize
        Do While cur.NextRow
            Dim m As Map
            m.Initialize
            m.Put("name", cur.GetString("name").ToLowerCase)
            m.Put("unique", cur.GetString("unique"))
            res1.Add(m)
        Loop  
        cur.close
    Catch
        Log(LastException)
    End Try
    Return res1
End Sub
 

Mahares

Expert
Licensed User
Longtime User
Here is a different method but yields similar results to @Mashiane's. It makes use of the sqlite_master table which holds the database schema.It shows how to determine the index name. It can also show you the create index SQL statement syntax:
B4X:
Dim DBTableName="Base_Wells" As String
Cursor1 = SQL1.ExecQuery2("SELECT name FROM sqlite_master WHERE type= ? AND tbl_name = ?", _
    Array As String("index",DBTableName))
    For i=0 To Cursor1.RowCount-1
        Cursor1.Position=i
'        Log($"${Cursor1.GetString("name")}  ${Cursor1.GetString("sql")} "$)    'Displays: index name and the create index syntax
        Log($"${Cursor1.GetString("name")}"$)    'Displays : the name of the index
    Next
 

Mashiane

Expert
Licensed User
Longtime User
Thanks, cool, I'm trying to use the same in my code to get the column names per index from the sql statement as I plan to use that in an app im working on. Was just busy today so havent concluded that. I like this as its more flexible..
 

Mashiane

Expert
Licensed User
Longtime User
'Description: Pad a string to the right with a specific character
'Tag: string, padding, right
B4X:
Sub PadRight(Value As String, MaxLen As Int, PadChar As String) As String
    Dim     intOrdNoLen = Len(Value)
    Dim i As Int
    For i = 1 To (MaxLen - intOrdNoLen) Step 1
           Value = PadChar & Value
    Next
    Return Value
End Sub
 

Mahares

Expert
Licensed User
Longtime User
Pad a string to the right with a specific character

@Mashiane:
You code should be like this:
B4X:
Sub PadRight(Value As String, MaxLen As Int, PadChar As String) As String
    Dim  intOrdNoLen As Int = Value.Length
    Dim i As Int
    For i = 1 To (MaxLen - intOrdNoLen) Step 1
        Value =  Value & PadChar
    Next
    Return Value   'displays: Mashianexxxxxxxxxxxx
End Sub
B4X:
Log(PadRight("mashiane", 20, "x") )    'displays: Mashianexxxxxxxxxxxx

But, if you really want to kill 2 birds with one stone and make it pad right or left, here is how I do it:
B4X:
Sub Pad(Value As String, MaxLen As Int, PadChar As String, right As Boolean) As String
    Dim  intOrdNoLen As Int = Value.Length
    Dim i As Int
    For i = 1 To (MaxLen - intOrdNoLen) Step 1
        If right Then
            Value =  Value & PadChar
        Else
            Value = PadChar & Value
        End If
    Next
    Return Value
End Sub
B4X:
Log($"${Pad("Mashiane", 20, "x", True)}"$)   'displays: Mashianexxxxxxxxxxxx
 

Mashiane

Expert
Licensed User
Longtime User
'Description: return the position of a map key
'Tag: map, key, indexof
B4X:
'return the position of a map key
private Sub MapGetKeyPos(strKey As String, mp As Map) As Int
    Dim kcnt As Int
    Dim ktot As Int = mp.Size - 1
    Dim kstr As String
    For kcnt = 0 To ktot
        kstr = mp.GetKeyAt(kcnt)
        If kstr.ToLowerCase = strKey.ToLowerCase Then
            Return kcnt
        End If
    Next
    Return -1
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
It should work also this way:

Dim m As Map
Dim lst As List = m.Keys
Return lst.IndexOf(strKey)

Without the kcnt loop.

(I suppose that you cannot write directly Return m.Keys.IndexOf(strKey) because Keys is an Array, not a List)
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…