I have a (Favourite_List)List containing Number of Long Type. If i use the command
B4X:
Favourite_List.Sort(True)
I get the ClasscastException
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
at java.lang.Long.compareTo(Long.java:32)
at java.util.ComparableTimSort.binarySort(ComparableTimSort.java:228)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:154)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:142)
at java.util.Arrays.sort(Arrays.java:1973)
at java.util.Collections.sort(Collections.java:1867)
at anywheresoftware.b4a.objects.collections.List.Sort(List.java:144)
at com.bayalu.KannadaSankalana.main._ulv_main_load(main.java:3898)
at com.bayalu.KannadaSankalana.main._get_selected_screen(main.java:1680)
at com.bayalu.KannadaSankalana.main._ulv_sidebar_itemclick(main.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA$2.run(BA.java:285)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5155)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
at miui.dexspy.DexspyInstaller.main(DexspyInstaller.java:171)
at dalvik.system.NativeStart.main(Native Method)
Is there any way to convert Long Type to Int type or vice versa?
I think you got an error because, for some reason, some entry in your list are strings.
'Sort' can work only with items of the same type.
Can you post the code which fills the list?
Sub Select_Items(ItemID As Long) As Boolean
Log("itemid - "&ItemID)
Dim exist_selected_item As Boolean
For i = 0 To Selected_Items.Size-1
If Selected_Items.Get(i) = ItemID Then
exist_selected_item = True
Exit
Else
exist_selected_item = False
End If
Next
If exist_selected_item Then
For i = 0 To Selected_Items.Size-1
If Selected_Items.Get(i) = ItemID Then
Log("item removed "&Selected_Items.Get(i))
Selected_Items.RemoveAt(i)
Exit
End If
Next
Else
Selected_Items.Add(ItemID)
End If
Return exist_selected_item
End Sub
First is better if you clean the code.
You can check if an item is already in list with
B4X:
Dim idxExist as int
IdxExist = Selected_Item.IndexOf(ItemID)
If IdxExist <> -1 then
Selected_Item.RemoveAt(idxExist)
Return true
Else
Selected_Item.add(ItemID)
End If
Ok, you have found the solution (sorting methods), but as Erel has posted, there is a string between your long. Therefore your code is not 'strong' and you should better check where this string enters in your list...
I have rechecked it many times. Infact to use the sort method everytime, i have to copy the contents of the list to an array of int and the sort the array and recopy the sorted array back into the list. So there is no string present.