Android Question Listview text color problem - Resolved

padvou

Active Member
Licensed User
Longtime User
I have a listview added from the designer.
I use a cursor to populate it with data:
B4X:
LVDb.Clear
    LVDb.TwoLinesLayout.Label.TextSize=20
    LVDb.TwoLinesLayout.Label.TextColor=Colors.Yellow
    LVDb.TwoLinesLayout.secondLabel.TextSize=16
    LVDb.TwoLinesLayout.secondLabel.TextColor=Colors.Cyan
    cursor2 = SQL2.ExecQuery(Query Text...")
    For i = 0 To cursor2.RowCount - 1
        cursor2.Position = i
        If cursor2.Getstring2(3)<>0 Then
            Log("1")
            LVDb.TwoLinesLayout.Label.TextColor=Colors.Yellow
            LVDb.AddTwoLines2("Text to be added...")
        Else if cursor2.Getstring2(3)=0 Then
            Log("2")
            LVDb.TwoLinesLayout.Label.TextColor=Colors.red
            LVDb.AddTwoLines2("Text to be added...")
        End If
    Next
    cursor2.close

The code will either add the first line in yellow text or red according to the if clause, which works correctly.
However, if the first line is yellow, the second is red and the third is yellow and I remove the second line, the third line which becomes second, after the removal, gets red text, although the code runs as it should, logs "1", which in the above example means it reads the value as "<>0".
If I then remove the second line, which is red (mistakenly) and add a new second line, it is still red!!
I point out that I don't just remove the line, but clear the listview and re-run the query, populate the cursor and re-populate the listview.
 

DonManfred

Expert
Licensed User
Longtime User
you can not have two different colors on one TYPE of item.
You can use OneLineLayout and TwoLineLayout to distinguish them.
But all items of the same type share the same layout (color).

Use CustomListView if you want to control more.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
You need to cheat if you want to use ListView to do this...

Use a TwoLinesLayout and a TwoLinesAndBitmap and then not have a bitmap... You can give the TwoLinesLayout Yellow colouring and the Bitmap ones Red for instance.

So in your code, something like this (untested)

B4X:
LVDb.Clear
 LVDb.TwoLinesLayout.Label.TextSize=20
 LVDb.TwoLinesLayout.Label.TextColor=Colors.Yellow
 LVDb.TwoLinesLayout.secondLabel.TextSize=16
 LVDb.TwoLinesLayout.secondLabel.TextColor=Colors.Cyan
 cursor2 = SQL2.ExecQuery(Query Text...")
For i = 0To cursor2.RowCount - 1
 cursor2.Position = i
If cursor2.Getstring2(3)<>0ThenLog("1")
 LVDb.TwoLinesLayout.Label.TextColor=Colors.Yellow
 LVDb.AddTwoLines2("Text to be added...")
Else if cursor2.Getstring2(3)=0ThenLog("2")
 LVDb.TwoLinesAndBitmap.Label.TextColor=Colors.red
 LVDb.AddTwoLinesAndBitmap("Text to be added...")
End If
Next
 cursor2.close
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Starting from B4A v6.80 there is a simpler solution.

SS-2017-03-06_17.03.52.png


B4X:
Dim cs As CSBuilder
ListView1.AddSingleLine(cs.Initialize.Color(Colors.Red).Append("Red").PopAll)
ListView1.AddSingleLine(cs.Initialize.Color(Colors.Green).Append("Green").PopAll)
ListView1.AddSingleLine(cs.Initialize.Color(Colors.Blue).Append("Blue").PopAll)

You can show as many colors as you like.
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
you can not have two different colors on one TYPE of item.
You can use OneLineLayout and TwoLineLayout to distinguish them.
But all items of the same type share the same layout (color).

Use CustomListView if you want to control more.

Adding a "DoEvents" line seems to make it work as I want it to, but I cannot explain why. Any ideas?
B4X:
LVDb.Clear
    LVDb.TwoLinesLayout.Label.TextSize=20
    LVDb.TwoLinesLayout.Label.TextColor=Colors.Yellow
    LVDb.TwoLinesLayout.secondLabel.TextSize=16
    LVDb.TwoLinesLayout.secondLabel.TextColor=Colors.Cyan
    cursor2 = SQL2.ExecQuery(Query Text...")
    For i = 0 To cursor2.RowCount - 1
        cursor2.Position = i
DoEvents
        If cursor2.Getstring2(3)<>0 Then
            Log("1")
            LVDb.TwoLinesLayout.Label.TextColor=Colors.Yellow
            LVDb.AddTwoLines2("Text to be added...")
        Else if cursor2.Getstring2(3)=0 Then
            Log("2")
            LVDb.TwoLinesLayout.Label.TextColor=Colors.red
            LVDb.AddTwoLines2("Text to be added...")
        End If
    Next
    cursor2.close
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
You need to cheat if you want to use ListView to do this...

Use a TwoLinesLayout and a TwoLinesAndBitmap and then not have a bitmap... You can give the TwoLinesLayout Yellow colouring and the Bitmap ones Red for instance.

So in your code, something like this (untested)

B4X:
LVDb.Clear
LVDb.TwoLinesLayout.Label.TextSize=20
LVDb.TwoLinesLayout.Label.TextColor=Colors.Yellow
LVDb.TwoLinesLayout.secondLabel.TextSize=16
LVDb.TwoLinesLayout.secondLabel.TextColor=Colors.Cyan
cursor2 = SQL2.ExecQuery(Query Text...")
For i = 0To cursor2.RowCount - 1
cursor2.Position = i
If cursor2.Getstring2(3)<>0ThenLog("1")
LVDb.TwoLinesLayout.Label.TextColor=Colors.Yellow
LVDb.AddTwoLines2("Text to be added...")
Else if cursor2.Getstring2(3)=0ThenLog("2")
LVDb.TwoLinesAndBitmap.Label.TextColor=Colors.red
LVDb.AddTwoLinesAndBitmap("Text to be added...")
End If
Next
cursor2.close

That's a nice workaround, but it shifts my line of text to the right, because of the (inexisting) bitmap.
Any idea how to make the bitmap width zero?
 
Upvote 0

eps

Expert
Licensed User
Longtime User
That's a nice workaround, but it shifts my line of text to the right, because of the (inexisting) bitmap.
Any idea how to make the bitmap width zero?

I think I remove the bitmap from the ListView..

Something like this...

B4X:
    ListView1.TwoLinesAndBitmap.ImageView.Visible = False
    ListView1.TwoLinesAndBitmap.Label.Left = 0
    ListView1.TwoLinesAndBitmap.SecondLabel.Left = 0
    ListView1.TwoLinesAndBitmap.ImageView.RemoveView
 
Upvote 0

eps

Expert
Licensed User
Longtime User
If you want to 'change' the colour on the fly you need to use two ListViews and flip between them as you change..

Alternatively CustomListView or something similar.
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
This is great:
Dim cs As CSBuilder
if OK THEN
ListView1.AddSingleLine(cs.Initialize.Color(Colors.Red).Append("Red").PopAll)
ELSE
ListView1.AddSingleLine(cs.Initialize.Color(Colors.Green).Append("Green").PopAll)
END IF
But,

How change the backcolor in each item (no fontcolor of label) and more than 2 posibilities.

And how set more than 2 posibilities using this other code (Layouts: TwoLineas as TwoLineasAnBitMap)
:

If cursor2.Getstring2(3)<>0ThenLog("1")
LVDb.TwoLinesLayout.Label.TextColor=Colors.Yellow
LVDb.AddTwoLines2("Text to be added...")
Elseif cursor2.Getstring2(3)=0ThenLog("2")
LVDb.TwoLinesAndBitmap.Label.TextColor=Colors.red
LVDb.AddTwoLinesAndBitmap("Text to be added...")
EndIf
 
Upvote 0
Top