0

Trying to educate myself from a sample Android app in Android Studio, I have found an ArrayList<String>.
In the debugging mode, the value(s) of this array is as below (using the view tool of the IDE)
I don't really understand how to recognize each element of the array.
I would appreciate any help.

The list is constructed as below (from a JSON file).

public static List<ArrayList<String>> getPrintDataFromJson(Context context, String jsonFileName, int copies, float multiple) {
    try {
        String jsonContent = AssetJsonReader.readJsonFromAssets(context, jsonFileName,
                    error -> Log.e(TAG, "Json not found: " + jsonFileName + ", error: " + error));

        if (jsonContent == null || jsonContent.isEmpty()) {
            Log.e(TAG, "JSON is empty: " + jsonFileName);
            return null;
        }
        return parseJsonToPrintData(jsonContent, copies, multiple);
    } catch (Exception e) {
        Log.e(TAG, "Cannot get data from Json: " + jsonFileName, e);
        return null;
    }
}

and...

private static List<ArrayList<String>> parseJsonToPrintData(String jsonContent, int copies, float multiple) {
        try {
            List<PrintTemplate> templates = null;

            try {
                PrintTemplate[] templateArray = JsonProcessor.GSON_INSTANCE.fromJson(jsonContent, PrintTemplate[].class);
                if (templateArray != null && templateArray.length > 0) {
                    templates = new ArrayList<>();
                    Collections.addAll(templates, templateArray);
                }
            } catch (Exception e) {
                try {
                    PrintTemplate template = JsonProcessor.GSON_INSTANCE.fromJson(jsonContent, PrintTemplate.class);
                    if (template != null) {
                        templates = new ArrayList<>();
                        templates.add(template);
                    }
                } catch (Exception ex) {
                    Log.e(TAG, "Unable to parse JSON as a PrintTemplate object or array", ex);
                    return null;
                }
            }

            if (templates == null || templates.isEmpty()) {
                Log.e(TAG, "There is no valid template data in the JSON.");
                return null;
            }

            List<String> processedJsonData = JsonProcessor.processTemplateList(templates);
            int templateLength = templates.size();
            List<PrinterImageProcessingInfoWrapper> infoWrappers = new ArrayList<>();

            for (int i = 0; i < templateLength; i++) {
                infoWrappers.add(createPrinterImageProcessingInfoWrapper(templates.get(i), copies, multiple));
            }
            List<String> processedInfoData = JsonProcessor.processInfoWrapperList(infoWrappers);

            List<ArrayList<String>> printData = new ArrayList<>();
            printData.add(new ArrayList<>(processedJsonData));
            printData.add(new ArrayList<>(processedInfoData));

            return printData;

        } catch (JsonSyntaxException e) {
            Log.e(TAG, "JSON syntax error", e);
            return null;
        } catch (Exception e) {
            Log.e(TAG, "Parsing JSON content failed to print data", e);
            return null;
        }
    }

and the result in the list is as below.

{
  "width" : 40.0,
  "height" : 20.0,
  "rotate" : 0,
  "background" : "",
  "backgroundImage" : "",
  "elements" : [ {
    "type" : "barcode",
    "x" : 2.0,
    "y" : 2.0,
    "width" : 36.0,
    "height" : 16.0,
    "rotate" : 0,
    "codeType" : 20,
    "value" : "6942141752896",
    "fontSize" : 3.200000047683716,
    "textHeight" : 3.200000047683716,
    "textPosition" : 0,
    "zIndex" : 0,
    "isTitle" : 0
    },{
    "type" : "text",
    "x" : 2.0,
    "y" : 2.0,
    "width" : 36.0,
    "height" : 7.400000095367432,
    "rotate" : 0,
    "value" : "Certificate",
    "fontCode" : "",
    "fontFamily" : "",
    "fontSize" : 5.599999904632568,
    "textAlignHorizonral" : 1,
    "textAlignVertical" : 1,
    "lineMode" : 9,
    "letterSpacing" : 0.0,
    "lineSpacing" : 1.0,
    "typesettingMode" : 1,
    "colorReverse" : 0,
    "zIndex" : 101,
    "wordSpacing" : 0,
    "lineBreakMode" : 1,
    "isTitle" : 0,
    "fontStyle" : [ "bold" ]
  } ]
New contributor
Dimitrios Georgiadis is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • 1
    The only thing you have posted is JSON. Where is your Java ArrayList<String>? Commented 20 hours ago
  • Please see stackoverflow.com/staging-ground/79921054. This question was promoted from staging ground while there were still open question. Commented 19 hours ago
  • Elliott, you are right. But here what I see ... private ArrayList<String> jsonList; Then.. List<ArrayList<String>> printData = JsonPrintData.getPrintData(this, printType, copies, multiple); for (int i = 0; i < length; i++){ jsonList.add(printData.get(0).get(i));} Commented 2 hours ago
  • and then...public static List<ArrayList<String>> getPrintData(Context context, String type,int copies, float multiple) { String jsonFileName === some json file; return getPrintDataFromJson(context, jsonFileName, copies, multiple); } So, the inital declaration is ArrayList<String> !! not json. Commented 2 hours ago

1 Answer 1

0

Ok, I found how to address the element.

((BarCodeElement) template.getElements().get(0)).getJson().setValue(MainActivity.tbar.getText().toString());
((TextElement) template.getElements().get(1)).getJson().setValue(MainActivity.tcod.getText().toString());
((TextElement) template.getElements().get(2)).getJson().setValue(MainActivity.tnam.getText().toString());
((TextElement) template.getElements().get(3)).getJson().setValue(MainActivity.tprc.getText().toString());
New contributor
Dimitrios Georgiadis is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.