Access tinydb inside extension developement

I am try to Creating Extension. and I need to access TinyDB Methods into my Extension. to Save and Retrieve Useful Extension Data.

I Try to import TinyDB as Follows but It Gives Me Following Error. i also Tried With super / this keywords and also try creating object using new key words. but all are giving me Something New Error.


I used Basic Extension Source with Following Source

//Basic Extension Code
import com.google.appinventor.components.runtime.TinyDB;
//Basic Extension Code 

@SimpleFunction(description = "XzBook")
public void ClearCache(String Cache) {


this.ClearTag(Cache);
}
//also tried
@SimpleFunction(description = "XzBook")
public void ClearCachex(String Cache) {
//error: constructor TinyDB in class TinyDB cannot be applied to given types;
TinyDB TDB = new TinyDB();
// [javac] TinyDB TDB = new TinyDB();
this.ClearTag(Cache);
}

After that i also want to store Tags and Values.

TinyDB TDB = new TinyDB(componentContainer);
TDB.ClearTag();
2 Likes

//imported inside source
import com.google.appinventor.components.runtime.ComponentContainer;

also i tried

TinyDB TDB = new TinyDB(ComponentContainer);

or

TinyDB TDB = new TinyDB(ComponentContainer x);

or i tried

TinyDB TDB = new TinyDB(container);

still producing error

Can you check your code and find if there is any instance (as parameter) of ComponentContainer ?

1 Like

Make a global instance variable,

private TinyBD tinyDb;

and initialize it inside the constructor,

public Xz(ComponentContainer container){
  super(container.$form());
  tinyDb = new TinyDB(container);
  tinyDb.Namespace("nameSpace"):
}

Now you can use the tinyDb variable from anywhere.

For example:

@SimpleFunction(description = "Clear Tag")
public void ClearTag(String tag) {
  tinyDb.ClearTag(tag);
}
package com.Xz;

import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.AndroidViewComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
//import com.google.appinventor.components.runtime.Component;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.util.YailList;
import com.google.appinventor.components.runtime.TinyDB;
import java.util.ArrayList;
import java.util.List;

@DesignerComponent(version = 1,
                   description = "X",
                   category = ComponentCategory.EXTENSION,
                   nonVisible = true,
                   iconName = "images/extension.png")
@SimpleObject(external = true)

public class Xz extends AndroidNonvisibleComponent{

private TinyDB tinyDb;


public Xz(ComponentContainer container){
        super(container.$form());
        tinyDb = new TinyDB(container);
        tinyDb.Namespace("nameSpace");



    }


    //TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
@SimpleFunction(description = "XzBook")
public void ClearCachex(String Cache) {

tinyDb.ClearTag(Cache);


}

THANKS ITS BUILD SUCCESSFUL

1 Like

I now marked @JEWEL 's contribution as solution, because it was him who provided it...

Taifun

2 Likes

It's important to note that TinyDB is just a wrapper around Android's SharedPreferences object. If you don't need additiona logic for managing the contents of a SharedPreferences instance, or need the App Inventor developer to have access to it, it might just be more portable to use the SharedPreferences directly.

1 Like

Note That : The solution is worked for me.
Thanks to : JEWEL

How can we access SharedPreferences Directly. I Don't Want to access TinyDB inside Extension Development.

Because i like to store Java Array inside SharedPreferences. i think Conversion from YailList to Java Array and Java Array to YailList it Problematic for me.

What about doing a Google search?

Taifun

2 Likes

any other simple way to store and get Java Array into TinyDB.
i think sharedPreference are just working like programatically text editor.

1 Like

How to write this code in extension.

WE CAN EASILY DO IN BLOCKS.

tinyDb.getDataValue(myTag,YailList.makeEmptyList())

But Its Not Working in Extension.

 @SimpleFunction(description = "Get Tag Value as and Empty YailList")
public void getTinyList(String myTag) {
  ArrayList<String> Ins = new ArrayList<>();

    Ins = getArr(tinyDb.getDataValue(myTag,YailList.makeEmptyList()));
    Ins.add("One");
    Ins.add("Two");

  tinyDb.StoreValue(myTag,YailList.makeList(Ins));
  
}

public  ArrayList<String> getArr(YailList values) {
    ArrayList<String> Strl = new ArrayList<>();
    for (String val : values.toStringArray()) {
      // parsed String
      String d = String.valueOf(val);
      Strl.add(d);
    }
    // and now you have the double list
    return Strl;
  }

Following Line Producing Error
Ins = getArr(tinyDb.getDataValue(Tabs,YailList.makeEmptyList()));

                ^

Any Solution ?

@Taifun 's extension

used SharedPreferences, you may want to have a look.

1 Like

This happens when you grow with copy-pasting. It is not a bad practice, but at least try to understand what code says before proceeding.

You should check Tiny DB source.

GetValue method accepts two parameters: key and default value

getDataValue accepts just one parameter: key

1 Like

Thanks to Reply . . .
You are right another reason of this problem bunks in practical in engineering collage. and also copy pasting in practical exam. Thanks to Support Back Bencher ! :slightly_smiling_face:

Can we use System.out.println("App Inventor \n"); while Writing Extension ? for Live Testing Purpose.

It won't work. That is not an android java function.

1 Like

What about using logcat?

Log.d(LOG_TAG, "hello world");

Taifun

1 Like
Log.d(LOG_TAG, "hello world");

this will displayed on any terminal ?

terminal ~ hello world

Taifun