I cannot create layout inside the json request

Asked

Viewed 39 times

-1

Hello!

In my project, I receive some fields via JSON, I have an empty layout, and I must create the elements (textview, imageview, edittext...) according to the data received from JSON.

JSON is receiving correctly, and outside of it, I can create any layout element programmatically. The problem is that within the JSON request, where I do the "if" checks to put elements, the application stops working.

this.handler.get(nmForm, new HttpJsonObjectListener() {

        @Override
        public void onRequestCompleted(final JSONObject object, Integer httpStatus, CharSequence msg) {

            TextView t = new TextView(getBaseContext());
            t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            t.setText("TextView");

            layout_form.addView(t);



            final JSONArray formFields = object.optJSONArray("formFields");

            for(int i = 0; i < formFields.length(); i++) {

                if(!tpRender.equals("HIDDEN")) {

                    Log.i("aanmLabel", nmLabel);
                    Log.i("tpRender", tpRender);
                    String qth = String.valueOf(qtHeight); 
                    Log.i("aaqtHeight", qth);
                    String qtw = String.valueOf(qtWidth); 
                    Log.i("aaqtWidth", qtw);

                    if(tpRender.equals("COMBO_BOX")) {
                        /*Spinner s2 = new Spinner(NewActivity.this);
                        s2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                        // e.setText("Button");
                        layout_form.addView(s2);*/

                    }

                }

            }
        }
    }, new HttpFailListener() {
        @Override
        public void onRequestCompleted(Exception e, Integer httpStatus, CharSequence msg) {
            Log.i("webview", "falhou ao obter json");
        }
    });

I just put out a textview as a test, and out of the verification, but it doesn’t work. Somebody help me?

1 answer

2

I managed to solve it. I programmatically declared the layouts within the Handler, and within

this.runOnUiThread(new Runnable() {
                        public void run() {

Browser other questions tagged

You are not signed in. Login or sign up in order to post.