0

私はこのコードを約 1 日いじっていますが、何が問題なのかわかりません。私はいくつかを受け取り、応答からJSON動的に作成しようとしています。Viewsコードを投稿してから、問題を説明します。

非同期タスク

private static final String TAG_SUCCESS = "success";
private static final String TAG_QUESTIONS = "questions";
private static final String TAG_NAME = "display_name";
private static final String TAG_FIELD = "field_type";
private static final String TAG_VALUE = "option_value"; 

private String r = "Radio";

class LoadAllQuestions extends AsyncTask<String, String, MyResult> {

    private ProgressDialog pDialog;

    JSONParser jParser = new JSONParser();
    JSONArray questions = null;
    MyResult theResult = null;

    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading questions. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected MyResult doInBackground(String... args) {

        // getting JSON string from URL
        companyName = cn.getText().toString();
        projectName = pn.getText().toString();
        String componentName = (String) ab.getSelectedTab().getText();

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("company", companyName));
        nameValuePairs.add(new BasicNameValuePair("project", projectName));
        nameValuePairs.add(new BasicNameValuePair("component",
                componentName));

        JSONObject json = jParser.makeHttpRequest(url, "POST",
                nameValuePairs);

        // Check your log cat for JSON response
        Log.d("All Questions: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                Log.v("RESPONSE", "Success!");
                // products found: getting Array of Questions
                questions = json.getJSONArray(TAG_QUESTIONS);

                // looping through All Questions
                for (int i = 0; i < questions.length(); i++) {

                    JSONObject c = questions.getJSONObject(i);

                    // Storing each JSON item in variable
                    String name = c.getString(TAG_NAME);
                    String field = c.getString(TAG_FIELD);
                    String value = c.getString(TAG_VALUE);

                    if (r.equals(field)) {
                        Log.v("RESPONSE", "oh look a radio button");
                        theResult = new MyResult();
                        theResult.setType(1);
                        theResult.setName(name);
                        theResult.setField(field);
                        theResult.setValue(value);

                    } else if (et.equals(field)){
                        Log.v("RESPONSE", "just another EditText");
                        theResult = new MyResult();
                        theResult.setType(2);
                        theResult.setName(name);
                        theResult.setField(field);
                        theResult.setValue(value);
                    } else {

                    }

                }
            } else {
                // no products found
                Log.v("ERROR", "No JSON for you!");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return theResult;
    }

    protected void onPostExecute(MyResult theResult) {
        // dismiss the dialog
        pDialog.dismiss();
        // if the answer should be a radio button, inflate it
        if (theResult.getType() == 1) {
            Log.v("RESPONSE", "About to create a radio button");
            // find
            LinearLayout content = (LinearLayout) view
                    .findViewById(R.id.genA_layout);
            // create
            TextView tv = new TextView(getActivity());
            RadioGroup rg = new RadioGroup(getActivity());
            rg.setOrientation(RadioGroup.HORIZONTAL);
            RadioButton rb = new RadioButton(getActivity());
            RadioButton rb2 = new RadioButton(getActivity());
            LinearLayout ll = new LinearLayout(getActivity());

            // set
            rb.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            rb2.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            ll.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            rb.setText(theResult.getValue());
            Log.v("RESPONSE", theResult.getValue());
            rb2.setText(theResult.getValue());              
            tv.setText(theResult.getName());
            Log.v("RESPONSE", theResult.getName());
            ll.setOrientation(LinearLayout.HORIZONTAL);
            // add
            rg.addView(rb);
            rg.addView(rb2);
            ll.addView(tv);
            ll.addView(rg);
            content.addView(ll);
        }
        // else inflate the view as an EditText field
        else if (theResult.getType() == 2) {
            Log.v("RESPONSE", "About to create an EditText");
            // find
            LinearLayout content = (LinearLayout) view
                    .findViewById(R.id.genA_layout);
            // create
            TextView tv = new TextView(getActivity());
            EditText et = new EditText(getActivity());
            LinearLayout ll1 = new LinearLayout(getActivity());
            // set
            tv.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            et.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            ll1.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            tv.setText(theResult.getName());
            Log.v("RESPONSE", theResult.getName());
            ll1.setOrientation(LinearLayout.HORIZONTAL);
            // add
            ll1.addView(tv);
            ll1.addView(et);
            content.addView(ll1);
        }

        // find
        LinearLayout loader = (LinearLayout) view
                .findViewById(R.id.loader_layout);
        Button save = (Button) view
                .findViewById(R.id.generalAssets_save_button_ID);
        // set
        loader.setVisibility(View.GONE);
        save.setVisibility(View.VISIBLE);

    };
}

XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gen_assets"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/twoglobe_line"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/genA_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/loader_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:gravity="center"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/info_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:gravity="center"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/company_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/company_name" />

                <EditText
                    android:id="@+id/company_input"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/info_layout1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/project_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/project_name" />

                <EditText
                    android:id="@+id/project_input"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10" />
            </LinearLayout>

            <Button
                android:id="@+id/generalAssets_load_button_ID"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/load" />

            <ListView
                android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:drawSelectorOnTop="false" >
            </ListView>
        </LinearLayout>

        <Button
            android:id="@+id/generalAssets_save_button_ID"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/save"
            android:visibility="gone" />
    </LinearLayout>

</ScrollView>

ログキャット

    06-03 16:04:51.948: E/json data(4103): json result {"questions":[{"display_name":"Store #","field_type":"Text Field","option_value":""},{"display_name":"Address","field_type":"Text Field","option_value":""},{"display_name":"Type of Business","field_type":"Drop Down Menu","option_value":"Education\r\nHealth\r\nComputers\r\nFood\r\nRetail\r\nOther"},{"display_name":"Is this business good?","field_type":"Radio","option_value":"Yes\r\nNo"},{"display_name":"Are they nice people?","field_type":"Check Box","option_value":"Yes\r\nNo"}],"success":1}
06-03 16:04:51.958: D/All Questions:(4103): {"success":1,"questions":[{"option_value":"","field_type":"Text Field","display_name":"Store #"},{"option_value":"","field_type":"Text Field","display_name":"Address"},{"option_value":"Education\r\nHealth\r\nComputers\r\nFood\r\nRetail\r\nOther","field_type":"Drop Down Menu","display_name":"Type of Business"},{"option_value":"Yes\r\nNo","field_type":"Radio","display_name":"Is this business good?"},{"option_value":"Yes\r\nNo","field_type":"Check Box","display_name":"Are they nice people?"}]}
06-03 16:04:51.958: V/RESPONSE(4103): Success!
06-03 16:04:51.958: V/RESPONSE(4103): just another EditText
06-03 16:04:51.958: V/RESPONSE(4103): just another EditText
06-03 16:04:51.958: V/RESPONSE(4103): oh look a radio button

問題は、どれもViews作成されていないことです。投稿からわかるようにlogcat、アプリケーションはJSON応答を取得し、それらを識別します。ただしonPostExecute()Views. @dmonが提案した変更を行った後、保存している結果が範囲外になると考える必要があります。theResult.getType()それ以外の場合、メソッドを呼び出すと、値が含まれます。現在、明らかにそうではありません。

問題を知っていると思っていても、その理由がわかりません。どんな助けや洞察も大歓迎です。

変更を表示するために編集

4

1 に答える 1