Android Question Java Code not working

ilan

Expert
Licensed User
Longtime User
hi

can someone please tell me why this does not work?

B4X:
Sub getfirstitemtop As Int
     Dim jo As JavaObject
     jo.InitializeContext
     Dim pos As Int = jo.RunMethod("topLv",Array(lv))
     Return pos
End Sub
'
#If Java
    public static int topLv(ListView mListView) {
        int index = mListView.getFirstVisiblePosition();
        View v = mListView.getChildAt(0);
        int top = (v == null) ? 0 : (v.getTop());
        return top;
    }
#End If

thanks

(source: https://stackoverflow.com/questions...-when-returning-to-a-listview/3035521#3035521)
 

stevel05

Expert
Licensed User
Longtime User
You didn't include the required imports for the 2 Views:

B4X:
#If Java
import android.widget.ListView;
import android.view.View;
    public static int topLv(ListView mListView) {
        int index = mListView.getFirstVisiblePosition();
        View v = mListView.getChildAt(0);
        int top = (v == null) ? 0 : (v.getTop());
        return top;
    }
#End If

But, in your code index is doing nothing, did you mean
B4X:
mListView.getChildAt(index);
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Hi Ilan
Can you give more hints? Does it crash, give incorrect results...?:)

Hi Jordi, this is what i get:

B4A Version: 8.50
Parsing code. (0.00s)
Building folders structure. (0.04s)
Compiling code. (0.02s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
Generating R file. (0.11s)
Compiling debugger engine code. (1.11s)
Compiling generated Java code. Error
B4A line: 100
End Sub
javac 1.8.0_91
src\b4a\example\main.java:613: error: cannot find symbol
public static int topLv(ListView mListView) {
^
symbol: class ListView
location: class main


You didn't include the required imports for the 2 Views:

B4X:
#If Java
import android.widget.ListView;
import android.view.View;
    public static int topLv(ListView mListView) {
        int index = mListView.getFirstVisiblePosition();
        View v = mListView.getChildAt(0);
        int top = (v == null) ? 0 : (v.getTop());
        return top;
    }
#End If

But, in your code index is doing nothing, did you mean
B4X:
mListView.getChildAt(index);

thanx a lot steve, it worked :) do i always have to add imports in Java??

Tip: use xCustomListView instead.

yes, i could use CLV or even ULV for that. ULV even got a simple example of what i am trying to implement. actually what i wanted to try is very simple i wanted to create a B4X drag able listview. and i wanted to use listview for that so i played yesterday a little bit with that. i really think that this is very missing in b4x.

so for ios it is very simple to implement but for android, its seems to be very complicated. tutorials i found:

Android:


ios:


what i have until now:

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
Private lv As ListView
Private selectedItemPos As Int = -1
Private selectedItemValue As Object
Private lasttoucheditem As Int

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    lv.Initialize("lv")
    Activity.AddView(lv,0,0,100%x,100%y)
 
    For i = 0 To 100
        lv.AddSingleLine("item : " & i)
    Next
 
    Dim ref As Reflector
    ref.Target = lv
    ref.SetOnTouchListener("lv_Touch")
End Sub

Sub lv_ItemLongClick (Position As Int, Value As Object)
    selectedItemPos = Position
    selectedItemValue = Value
End Sub

Private Sub lv_Touch (o As Object, ACTION As Int, x As Float, y As Float, motion As Object) As Boolean
    If ACTION = 0 Then
        lasttoucheditem = Floor(y/lv.SingleLineLayout.ItemHeight)
    Else if ACTION = 2 Then
        If selectedItemPos > -1 Then
            If Floor(y/lv.SingleLineLayout.ItemHeight) <> lasttoucheditem Then
                lasttoucheditem = Floor((y+getfirstitemtop)/lv.SingleLineLayout.ItemHeight)
                updateLV(lasttoucheditem)          
            End If
        End If
    else if ACTION = 1 Then
        selectedItemPos = -1
    End If
    Return False
End Sub

Sub getfirstitemtop As Int
     Dim jo As JavaObject
     jo.InitializeContext
     Dim pos As Int = jo.RunMethod("topLv",Array(lv))
     Log(pos)
     Return Abs(pos)
End Sub
'
#If Java
import android.widget.ListView;
import android.view.View;
    public static int topLv(ListView mListView) {
        View v = mListView.getChildAt(0);
        int top = (v == null) ? 0 : (v.getTop());
        return top;
    }
#End If

Sub updateLV(pos As Int)
    Dim l As List
    l.Initialize
    For i = 0 To lv.Size-1
        Dim item As Object = lv.GetItem(i)
        l.Add(item)
    Next
    lv.Clear
 
    l.RemoveAt(selectedItemPos)
    l.InsertAt(pos+getfirstvisibleItem,selectedItemValue)
    selectedItemPos = pos+getfirstvisibleItem
 
    For j = 0 To l.Size-1
        lv.AddSingleLine(l.Get(j))
    Next
End Sub

Sub getfirstvisibleItem As Int
    Try
        Dim ref As Reflector
        ref.Target = lv  
        Return(ref.RunMethod("getFirstVisiblePosition"))
    Catch
        Return(0)
    End Try
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

(LongClick and move the item, it works but need some improvements :) )
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
yes, i could use CLV or even ULV for that. ULV even got a simple example of what i am trying to implement. actually what i wanted to try is very simple i wanted to create a B4X drag able listview. and i wanted to use listview for that so i played yesterday a little bit with that. i really think that this is very missing in b4x.
You should definitely not use ListView for this. If ULV already supports it then use it. If not you can implement it with xCustomListView.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
You should definitely not use ListView for this. If ULV already supports it then use it. If not you can implement it with xCustomListView.

yes ULV supports it. how about b4i? any chance we could get a working example using Table View?
 
Upvote 0
Top