save list to csv

droidman

Member
Licensed User
Longtime User
I listed all the contacts i have on the phone on a list, now i wanted to save the list to CSV but i always get errors on the emulator.
Dim su As StringUtils
su.SaveCSV(File.DirRootExternal, "1.csv", ",", table)
i'm doin'g it like on the tutorial's you have, with savecsv and savecsv2 and none seems to work. is it a bug on the emulator?
 

droidman

Member
Licensed User
Longtime User
Have you added an SD card to the emulator when you created it?

yes mate it has a SD card mounted and on my mobile phone (htc magic) i get the same error:

B4X:
An Error has ocurred in sub: main_savecontacts_click (B4A line: 80) su.SaveCSV(File.DirRootExternal, "1.csv", ",",list1) java.lang.ClassCastException java.lang.string Continue ?
 
Upvote 0

droidman

Member
Licensed User
Longtime User
Something in your list can't be converted to a String. Look in the Logs tag, unfiltered, and you may get more information from the stack trace there.

i cant understand half of it, also i dont know how to copy those lines to past here... is that log saved anywhere?
 
Upvote 0

droidman

Member
Licensed User
Longtime User
It's not saved except on the device or emulator. You can take a screenshot of the IDE or right click on the Logs to copy lines to the clipboard.

great. here it is:
B4X:
java.lang.ClassCastException: java.lang.String
   at anywheresoftware.b4a.objects.StringUtils.SaveCSV2(StringUtils.java:74)
   at
anywheresoftware.b4a.objects.StringUtils.SaveCSV(StringUtils.java:67)
   at contacts2.csv.droidman.main._savecontacts_click(main.java:277)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:99)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:89)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:85)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:49)
   at android.view.View.performClick(View.java:2485)
   at android.view.View$PerformClick.run(View.java:9080)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3647)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.ClassCastException: java.lang.String

this is just the last exception
 
Upvote 0

droidman

Member
Licensed User
Longtime User
and this one is on my htc magic
B4X:
LogCat connected to: B4A-Bridge: HTC HTC Magic/Dream-##########
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
Connected to B4A-Bridge
Installing file.
PackageAdded: package:convert.kgm.droidman
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
main_savecontacts_click (B4A line: 80)
su.SaveCSV(File.DirRootExternal, "1.csv", ",", list1)
java.lang.ClassCastException: java.lang.String
   at anywheresoftware.b4a.objects.StringUtils.SaveCSV2(StringUtils.java:74)
   at anywheresoftware.b4a.objects.StringUtils.SaveCSV(StringUtils.java:67)
   at convert.kgm.droidman.main._savecontacts_click(main.java:277)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:99)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:89)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:85)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:49)
   at android.view.View.performClick(View.java:2501)
   at android.view.View$PerformClick.run(View.java:9107)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3848)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.ClassCastException: java.lang.String
 
Upvote 0

droidman

Member
Licensed User
Longtime User
:sign0104:
I take that back, I was confusing two different Get methods. Are you sure you have only Strings in your List?

right now the list contains the person name and their e-mail.
if one of the variables is not a string i can always modify it to become a string right?
 
Upvote 0

droidman

Member
Licensed User
Longtime User
From the help for SaveCSV
I suspect your list items are not arrays.

ok mate so this is how i load the list:

B4X:
For i = 0 To listOfContacts.Size - 1
        myContact = listOfContacts.Get(i)
        list1.Add(myContact.DisplayName)
        map1.Put(myContact.DisplayName, myContact)
    Next
    list1.Sort(True)
    For i = 0 To list1.Size - 1
                myContact = map1.Get(list1.Get(i))
                emails1 = myContact.GetEmails
      emails = emails1
                emails = emails.Replace ("{", "")
      emails = emails.Replace ("}", "")
      emails = emails.Replace ("=1", "")
      names = myContact.Name
                lvPersons.AddTwoLines(names,emails)

and then when i do this when the button is pressed to save:
B4X:
su.SaveCSV(File.DirDefaultExternal, "1.csv", ",", list1)
i also tried
B4X:
su.SaveCSV(File.DirDefaultExternal, "1.csv", ",", lvPersons)
it fails. do you find anything wrong with my code?
i'm just starting i have less than 4 days of basic4android.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
do you find anything wrong with my code?
Yes, like I pointed out your list items are strings, not string arrays which is what SaveCSV expects.

list1.Add(myContact.DisplayName)

is adding a string as a list item.

You need to add a string array

list1.add(Array As String(myContact.DisplayName))
 
Upvote 0

droidman

Member
Licensed User
Longtime User
Yes, like I pointed out your list items are strings, not string arrays which is what SaveCSV expects.

list1.Add(myContact.DisplayName)

is adding a string as a list item.

You need to add a string array

list1.add(Array As String(myContact.DisplayName))

thanks for your help. I did that modification and now the program "explodes" at "list1.Sort(True)" probably because the emulator restarted and now it does not have the contacts i created before.
so this validation could be done in 2 ways i did know in vb and .net wich are checking if the list contains any ittems and then validating and the other one was the BAD way to do it that was "on error resume next"
wich by the way, is there any way to do that on basic4android ?
 
Upvote 0
Top