Android Code Snippet Sharing the goodness: Some useful methods

Mashiane

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)
Would be nice though of such a method existed, makes things easy. Also the case sensitivity now becomes another issues as the keys are CaseSensiTive.
 

Mahares

Expert
Licensed User
Longtime User
If kstr.ToLowerCase = strKey.ToLowerCase
As you mentioned in your previous post, Keys are case sensitive. You should replace this:
B4X:
If kstr.ToLowerCase = strKey.ToLowerCase Then
with this:
B4X:
If kstr = strKey Then

Suppose I have this map:
B4X:
Dim Mm As Map
    Mm.Initialize
    Mm.Put("Sigla", "AA")
    Mm.Put("Provincia", "Prima provincia")
    Mm.Put("Ipt", "30%")
    Mm.Put("Perc", "30")
    Mm.Put("IPT", "33%")

Log(MapGetKeyPos("IPT", Mm) ) 'will always return the 2 with your code. Whereas if you remove the case validation, it will find the correct one which is 4.
Please provide examples when you post snippets, so the rest of us can better understand. You have a very good thing going. Thank you for your benevolence.
 

Mashiane

Expert
Licensed User
Longtime User
I've learned the hard way during my scenarios that it's just better to lowercase my map keys. They can create havoc if not handled with care (not the maps, but how you code them)
 

Mashiane

Expert
Licensed User
Longtime User
The attached bal file is for those ones who need TwoLinesAndBitMap for CustomView controls.

B4X:
Sub TwoLinesAndBitmapSetting(LabelTextColor As Int, LabelSize As Int, SecondLabelTextColor As Int, SecondLabelSize As Int)
    FirstLabel.TextColor = LabelTextColor
    FirstLabel.TextSize = LabelSize
    SecondLabel.TextColor = SecondLabelTextColor
    SecondLabel.TextSize = SecondLabelSize
End Sub

Sub AddTwoLinesAndBitmap2(LabelText As String, SecondLabelText As String, bmp As Bitmap, Index As Object) As Panel
    Dim pan As Panel
    pan.Initialize("")
    Activity.AddView(pan,0dip,0dip,100%x,100%y)
    pan.LoadLayout("vTwoLinesAndBitMap")
    pan.Color = Colors.Transparent
    FirstLabel.Text = LabelText
    SecondLabel.Text = SecondLabelText
    TwoLinesAndBitmapSetting(Colors.Black,16dip,Colors.Black,13dip)
    Image.BitMap = bmp
    pan.removeView
    pan.Tag = Index
    Return pan
End Sub

This could be useful in the ImageLoader functionality.

Usage:

B4X:
Dim pnl As Panel = AddTwoLinesAndBitmap2("Label1", "SecondLabel", LoadBitMap(File.DirAssets,"x.png"),"mykey")
        lstItems.Add(pnl,"90dip","mykey")

The height of the twolinesbitmap in b4a is 90dip and the label sizes are 19.5 and 16.5 respectively. Also note the bal script contents for more.

That's all folks.
 

Attachments

  • vtwolinesandbitmap.bal
    2.5 KB · Views: 401
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Making my CustomListViewWork...

I wanted to set badges within the views inside my customview, however these were not working by finding the label to set a badge on, thus I needed to remove and create the items. I adjusted my CustomListView to have this method..



B4X:
'Adds a custom item at the specified index.
Public Sub ReplaceAt(Index As Int, pnl As Panel, ItemHeight As Int, Value As Object)
    RemoveAt(Index)
    InsertAt(Index, pnl, ItemHeight, Value)
End Sub

And my up and down code...

B4X:
Sub imgUp_Click
    Dim imgV As ImageView
    imgV = Sender
    msa.with(mst.Wobble).duration(500).playon(imgV)
    Dim idx As Int = lstRecordsC.GetItemFromView(imgV)
    Dim pnl As Panel = lstRecordsC.GetPanel(idx)
    jobMap = pnl.Tag
    Item_no = jobMap.Get("Item_no")
    Qty = jobMap.Get("qty")
    Qty = Qty + 1
    jobMap.Put("qty",Qty)
    pnl = AddItem(jobMap)
    lstRecordsC.ReplaceAt(idx,pnl,lstRecordsC.DefaultItemHeight,Item_no)
End Sub

Sub imgDown_Click
    Dim imgv As ImageView
    imgv = Sender
    msa.with(mst.wobble).duration(500).playon(imgv)
    Dim idx As Int = lstRecordsC.GetItemFromView(imgv)
    Dim pnl As Panel = lstRecordsC.GetPanel(idx)
    jobMap = pnl.Tag
    Item_no = jobMap.Get("Item_no")
    Qty = jobMap.Get("qty")
    If Qty > 0 Then
        Qty = Qty - 1
        jobMap.Put("qty",Qty)
        pnl = AddItem(jobMap)
        lstRecordsC.ReplaceAt(idx,pnl,lstRecordsC.DefaultItemHeight,Item_no)
    End If
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
SQLite Things:

I wanted to update some related table columns from one table based on a foreign key in another.

B4X:
sqlCommands.add("update ProgrammesSet set ppBudget = (select IYMAnalysisSet.Projections from IYMAnalysisSet where IYMAnalysisSet.IYMAnalysis_Programmes = ProgrammesSet.Number) where exists (select * from IYMAnalysisSet where IYMAnalysisSet.IYMAnalysis_Programmes = ProgrammesSet.Number)")
    sqlCommands.add("update ProgrammesSet set ppExpenditure = (select IYMAnalysisSet.Actual from IYMAnalysisSet where IYMAnalysisSet.IYMAnalysis_Programmes = ProgrammesSet.Number) where exists (select * from IYMAnalysisSet where IYMAnalysisSet.IYMAnalysis_Programmes = ProgrammesSet.Number)")

ProgrammesSet is related to IYMAnalysis set by IYMAnalysis_Programmes = Number. So this updates the ppBudget and ppExpenditure columns in ProgrammesSet based on Projections and Action from the IYMAnalysisSet table.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…