Android Question listview checked not working (reflection, selector,...)

Controller

Member
Licensed User
Longtime User
Anyone familier with the listview internals and can give me a clue why my code is not working?
Or is it generally impossible for checked (checked status work, but isn't displayed as such?

Excerpt:
B4X:
Sub ListView_MakeCheckable(xListView As ListView, xDrawable As Object, MultiCheck As Boolean)
    Dim xRef1 As Reflector
    Dim xInt1 As Int
    Dim xArgs(1) As Object
    Dim xTypes(1) As String

    xRef1.Target = xListView
    If MultiCheck Then
      xInt1 = 2
        Else
        xInt1 = 1
        End If
    xRef1.RunMethod2("setChoiceMode", xInt1, "java.lang.int")
    xArgs(0) = xDrawable
    xTypes(0) = "android.graphics.drawable.Drawable"' StateListDrawable"
    xRef1.RunMethod4("setSelector", xArgs, xTypes)
    End Sub

Sub ListView_SetItemChecked(xListView As ListView, Position As Int, SetChecked As Boolean)
    Dim xRef1 As Reflector

  Log("Selecting: " & Position & "-> " & SetChecked)
    xRef1.Target = xListView
    xRef1.RunMethod3("setItemChecked", Position, "java.lang.int", SetChecked, "java.lang.boolean")
  End Sub

<?xml version="1.0" encoding="utf-8"?>
<selector android:id="@+id/myselector" xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/listviewsel_selected" />
    <item android:state_pressed="true" android:drawable="@drawable/listviewsel_selected" />
    <item android:state_checked="true" android:drawable="@drawable/listviewsel_marked" />
    <item android:state_hovered="true" android:drawable="@drawable/listviewsel_hovered" />
    </selector>
 

Attachments

  • test.zip
    8.7 KB · Views: 153

eps

Expert
Licensed User
Longtime User
The main issue with ListView is that once you have set it up you can't really change it, all the settings apply to all of the items in the ListView.

Maybe try this http://www.b4x.com/android/forum/threads/class-checklist.18853/

?

You probably could fiddle the ListView, but you would need two ListViews and two variations of the ListView (i.e. addsingleline and addsinglelinebitmap) and then one of those - the bitmap one, displays the checkbox - but it's a really clunky implementation. Then flip flop between the two ListViews, to show the update to the User. This works, I've tried something similar before, but it's a bit of a hammer to crack a nut.
 
Upvote 0

Controller

Member
Licensed User
Longtime User
scrollview is not an option.
there are some custom listviews around, but typically overloaded and/or not working properly... - not an option
the only thing I want to do is to have a different background color for checked items, which should be possible with the proper StateList, at least for the pure android listviews, and I worry if the item layouts break that compatibility / need to support this behavior.
SetItemChecked and GetItemChecked work fine internally, and toggle automatically on user interaction, but the background color does not change;
However, when pressed, the new specified color for pressed state works, so the selector should be fine, too.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
If all you want to do is change the background colour, then you can do this using the method I outlined above...

Two ListViews and two methods of adding the items to the ListView.

such as (not actual code, but should give you the idea)

B4X:
ListView1.TwoLinesLayout.Background = Colors.Black

ListView1.TwoLinesAndBitmap.Background = Colors.Grey

If checked
    ListView1.AddTwoLines2(etc)
else
    ListView1.AddTwoLinesAndBitmap2(etc)
end if
 
Last edited:
Upvote 0

Controller

Member
Licensed User
Longtime User
Guess I have to wait for somebody making a listview with multiple layouts, or custom draw callback, or at least supporting checked states/statelistdrawables.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
ListView will never have multiple layouts, it can only have 1 or at most 2 layouts (with a bit of fiddling)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…