Need help in making a simple extension (return a list)

this is wrong

Oh I just asked him can we do like this

no you can not do

Ok...... :pray:

Please wait, I'm trying to make what you said

I returned

i totally recommend that but he should know some basic like return type, data types, type casting, some basic keywords like(public private etc..) because extension development is not limited to sum of the number and basic java program.

1 Like

Yeah, you are right @preetvadaliya :grin:

Finally where is @Chiru. :sweat_smile:

1 Like

@Alpha2020 Here you go

For Get List :

import com.google.appinventor.components.runtime.util.YailList;
import java.util.ArrayList;
import java.util.List;

................
.........
..............

    @SimpleFunction(description = "")
    public YailList GetList() {
        List listItems = new ArrayList();
        listItems.add("Text1");
        listItems.add("Text2");
        listItems.add("Text3");
        return YailList.makeList(listItems);
    }
1 Like

Thank you very very much..... :pray: :smiling_face_with_three_hearts:

1 Like

You're welcome @Alpha2020

1 Like

Note that you can also return type List<> directly, for example:

public List<String> GetItems() {
    return Arrays.asList("Item1", "Item2", "Item3");
}

The App Inventor runtime will take care to ensure that every value returned in the list is coercible to a valid type in the blocks editor.

If you return YailList, you are expected to ensure that every item in the list is valid.

3 Likes

Okay, Thank you Mr. @ewpatton :wink:

1 Like

Sorry to drag this one up from the deep.....

Is it possible to return a list of lists (list of pairs)? For example like this:

return Arrays.asList(("Item1",1), ("Item2",2), ("Item3",3));

My IDE doesn't like the syntax....

maybe? -

return Arrays.asList(Arrays.asList("Item1",1), Arrays.asList("Item2",2), Arrays.asList("Item3",3));

Hope i did not misunderstood your question or gave a incorrect answer :sweat_smile:

1 Like

Well the IDE likes it, will have to build and see what comes back :slight_smile:

1 Like

and it comes back as a list of lists :smiley:

@SimpleProperty(description = "list of mimetypes")
  public List Mimetypes() {
    return Arrays.asList(Arrays.asList("PNG", "image/png"),Arrays.asList("JPG", "image/jpg"),Arrays.asList("BMP", "image/bmp"));
  }

(only problem is, have you seem how long the mimetypes list is :upside_down_face: - not really a problem, I will need to use find and replace anyway)

1 Like

If you have the mime list as a CSV file you could read it into the extension and pass its contents through CsvUtil.fromCsvTable (which is the what the list from csv table block does).

2 Likes

Like so ?

@SimpleProperty(description = "list of mimetypes")
    public List Mimetypes() {
      return CSvUtil.fromCsvTable("PNG", "image/png"\n"JPG", "image/jpg"\n"BMP", "image/bmp");
    }