Recover TinyDB saves with Java and Android Studio

643 views
Skip to first unread message

Andrea Barcaro

unread,
Feb 24, 2016, 7:24:35 PM2/24/16
to App Inventor Open Source Development
Hi at all,

I'm trying to update my application previously made with App Inventor. In that application I used the TinyDB component.
Now the app is written in Java with Android Studio and I used again TinyDB from this github (https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo/blob/master/TinyDB.java). I can save and delete savedata successfully.

What I'd like to do is recover my savefiles from the App Inventor TinyDB. The problem is that with App Inventor I saved this:
tag: "actualSavefiles"
valueToStore: make a list(list of double, list of double, list of String)

How can I recover this TinyDB with Java TinyDB?

The problem is that I don't know what is the the class of a list stored with App Inventor and containing a list of double, a list of double and a list of String.
I mean, in Java an ArrayList of string object is List<String>. 
With SharedPreferences I can get the saves but I can't find what is that class (and from there I could know how to export double and string values).

Thanks for the attention,
Andrea

Ghica

unread,
Feb 25, 2016, 4:45:55 AM2/25/16
to App Inventor Open Source Development
If you still have the AI source code, maybe you make a new version that exports the TinyDB as one or more CSV files. You could then import that CSV file to your new Java app.
Cheers, Ghica.

Jos Flores

unread,
Feb 25, 2016, 5:07:17 AM2/25/16
to app-inventor-open-source-dev
I had never seen that TinyDB implementation. You will probably have to
ask the owner of the github repo how to use it.
It seems to be a very similar implementation to the one in App
Inventor, but no idea if it is supposed to be compatible.

The AI component stores Objects, using a couple of JSON util methods.
Here's where/what it returns:
https://github.com/mit-cml/appinventor-sources/blob/master/appinventor/components/src/com/google/appinventor/components/runtime/TinyDB.java#L115

and here is the JSON util method used:
https://github.com/mit-cml/appinventor-sources/blob/master/appinventor/components/src/com/google/appinventor/components/runtime/util/JsonUtil.java#L194

You may be able to figure out what's returning by following the links above.

cheers,
José
> --
> You received this message because you are subscribed to the Google Groups
> "App Inventor Open Source Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to app-inventor-open-so...@googlegroups.com.
> To post to this group, send email to
> app-inventor-o...@googlegroups.com.
> Visit this group at
> https://groups.google.com/group/app-inventor-open-source-dev.
> For more options, visit https://groups.google.com/d/optout.

Andrea Barcaro

unread,
Feb 26, 2016, 9:54:58 AM2/26/16
to App Inventor Open Source Development
thanks to all for reply.

First of all I imported "kawa-2.0.jar" and from the source of App Inventor: ArrayIndexOutOfBoundsError.java, IllegalArgumentError.java, JsonUtil.java, Lists.java, RuntimeError.java, UninitializedInstanceError.java, YailConstants.java, YailList.java, YailNumberToString.java, YailRuntimeError.java.

Then I modified the TinyDB.java from the git in this part:
public TinyDB(Context appContext) {
//Before it was preferences = PreferenceManager.getDefaultSharedPreferences(appContext);
    //In this way with "TinyDB1" the place where tags are saved is the same with the one used in App Inventor
preferences=appContext.getApplicationContext().getSharedPreferences("TinyDB1", Context.MODE_PRIVATE);
}

and I paste in this TinyDB.java this code:

public Object GetValue(final String tag, final Object valueIfTagNotThere) {
try {
String value = preferences.getString(tag, "");
// If there's no entry with tag as a key then return the empty string.
// was return (value.length() == 0) ? "" : JsonUtil.getObjectFromJson(value);
return (value.length() == 0) ? valueIfTagNotThere : JsonUtil.getObjectFromJson(value);
} catch (JSONException e) {
throw new YailRuntimeError("Value failed to convert from JSON.", "JSON Creation Error.");
}
}

Then in my app I did:

//Initialize the variables
List<String> listone;
List<String> listtwo;
List<Double> listthree;
Object save;
//Get the saves
tinydb=new TinyDB(this);
save = tinydb.GetValue("actualSavefiles","");
//Create now the ArrayList containing the savefiles
ArrayList al1 = (ArrayList) save;
//Put your App Inventor saves in your variables
listone=(ArrayList)al1.get(0);
listtwo=(ArrayList)al1.get(1);
listthree=(ArrayList) al1.get(2);

Hope that this could be useful to someone
thanks for the inputs;)
Reply all
Reply to author
Forward
0 new messages